How to create a zip of folder and sub folders using java.util.zip
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Deval_Parikh
Posted On:   Thursday, November 30, 2006 08:28 PM

Hi i am using java.util.zip to create a zip. My problem is my code is just creating a zip of current folder and not the sub directories and files in the subdirectories. My code is as under: public class Zip { static final int BUFFER = 2048; public static void main (String argv[]) { try { 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 File   More>>

Hi i am using java.util.zip to create a zip.


My problem is my code is just creating a zip of current folder and not the sub directories and files in the subdirectories.


My code is as under:


			public class Zip {
			
static final int BUFFER = 2048;
public static void main (String argv[]) {
try {
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
File f = new File(".");
String files[] = f.list();

for (int i=0; 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();
}
}
}




What changes should i make in this code to do so.

thanks

   <<Less

Re: How to create a zip of folder and sub folders using java.util.zip

Posted By:   WarnerJan_Veldhuis  
Posted On:   Friday, December 1, 2006 05:37 AM

You don't show any exceptions, so I have to guess this one: you are missing a out.closeEntry():

while((count = origin.read(data, BUFFER)) != -1) {
out.write(data, 0, count);
}
out.closeEntry();
origin.close();
About | Sitemap | Contact