I am using java.util.zip to zip up some files in java. The problem that I am having is that the zipped file will create folders for all the parent directories. For example, if I have a file at C:/folder1/folder2/folder3/file.txt
This will create a zipped file with a folder named folder 1, then folder 2, and so on until file.txt.
The result that I am looking for is a zipped folder with just the file.txt file at the root without any folders.
Here is some sample code
BufferedInputStream origin = null;
FileOutputStream dest = new
FileOutputStream("c:\\zip\\myfigs.zip");
ZipOutputStream out = new ZipOutputStream(new
BufferedOutputStream(dest));
//out.setMethod(ZipOutputStream.DEFLATED);
byte data[] = new byte[BUFFER];
// get a list of files from current directory
String files[] = {"C:/folder1/folder2/folder3/file.txt"};
for (int i=0; i<files.length; i++) {
System.out.println("Adding: "+files[i]);
FileInputStream fi = new
FileInputStream(files[i]);
origin = new
BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(files[i]);
out.putNextEntry(entry);
int count;
while((count = origin.read(data, 0,
BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
out.close();
} catch(Exception e) {
e.printStackTrace();
}