Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

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

807607Nov 30 2006 — edited Nov 30 2006
Hi i am using the java.util.zip to create the zip.

My problem is the code makes the zip of the current directories and not the subdirectories 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<files.length; i++) {
                                  System.out.println("Adding: "+files);
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 do i need to make in this code so that it creates a zip of subdirectories as well.

Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 28 2006
Added on Nov 30 2006
2 comments
450 views