4 Replies Latest reply: Oct 14, 2008 2:00 PM by 843785 RSS

    Zipping folders recursively

    843785
      Hello. I'm new to Java.I have a program that zips a directory recursively, but the problem is with the empty folders.I need to keep the structure of the directory, even if there are empty folders.Can anyone help me, please?
      Thank u in advance!

      import java.io.BufferedReader;
      import java.io.File;
      import java.io.FileInputStream;
      import java.io.FileOutputStream;
      import java.io.InputStreamReader;
      import java.util.zip.ZipEntry;
      import java.util.zip.ZipOutputStream;

      public class Zipping {

      public static void main(String[] a) throws Exception {

      BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
      System.out.println("Enter the source directory/file name : ");
      String source = in.readLine();
      File src = new File (source);

      if (src.isDirectory()) {
      String zipFile = source + ".zip";
      zipFolder (source, zipFile);
      }
      else {
      int lastDot = source.lastIndexOf(".");
      String zipFile;
      if (lastDot != -1) {
      zipFile = source.substring(0, lastDot) + ".zip";
      }
      else {
      zipFile = source + ".zip";
      }
      zipFolder (source, zipFile);
      }
      }

      static public void zipFolder(String srcFolder, String destZipFile) throws Exception {
      ZipOutputStream zip = null;
      FileOutputStream fileWriter = null;

      fileWriter = new FileOutputStream(destZipFile);
      zip = new ZipOutputStream(fileWriter);

      addFileToZip("", srcFolder, zip);
      zip.flush();
      zip.close();
      }

      static private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws Exception {

      File folder = new File(srcFile);

      if (folder.isDirectory()) {
      addFolderToZip(path, srcFile, zip);
      }
      else {
      byte[] buf = new byte[1024];
      int len;
      FileInputStream in = new FileInputStream(srcFile);
      zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));

      while ((len = in.read(buf)) > 0) {
      zip.write(buf, 0, len);
      }
      }
      }

      static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws Exception {
      //if ( src.isDirectory()) { 
      File folder = new File(srcFolder);
      String[] fileName = folder.list();

      for (int i = 0; i < fileName.length; i++) {

      if (path.equals("")) {
      addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip);
      }
      else {
      addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName[i], zip);
      }
      }
      }
      }
        • 1. Re: Zipping folders recursively
          800308
          Reea,

          Repost your code between &#123;code} tags... it makes it much easier to read.
          • 2. Re: Zipping folders recursively
            843785
            I hope this is better:

            import java.io.BufferedReader;
            import java.io.File;
            import java.io.FileInputStream;
            import java.io.FileOutputStream;
            import java.io.IOException;
            import java.io.InputStreamReader;
            import java.util.zip.ZipEntry;
            import java.util.zip.ZipOutputStream;
            
            
            public class Zipping {
            
            
            public static void main(String[] args) throws Exception {
            
            BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
            System.out.println("Enter the source directory/file name : ");
            String source = in.readLine();    
            File src = new File (source);
            
            
            if (src.isDirectory()) { 
               String zipFile = source + ".zip";
               zipFolder (source, zipFile);
            }
            else {
                 int lastDot = source.lastIndexOf(".");
                 String zipFile;
                 if (lastDot != -1) {
                    zipFile = source.substring(0, lastDot) + ".zip";  
                 }
                 else {
                      zipFile = source + ".zip";
                 }
            zipFolder (source, zipFile);    
            }
            }
            
            
            static public void zipFolder(String srcFolder, String destZipFile) throws Exception {
                ZipOutputStream zip = null;
                FileOutputStream fileWriter = null;
            
            
                fileWriter = new FileOutputStream(destZipFile);
                zip = new ZipOutputStream(fileWriter);
            
            
                addFileToZip("", srcFolder, zip);
                zip.flush();
                zip.close();
              }
            
            
              static private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws Exception {
            
            
                File folder = new File(srcFile);
            
                if (folder.isDirectory()) { 
                    addFolderToZip(path, srcFile, zip);
                }
                else {
                     byte[] buf = new byte[1024];
                     int len;
                     FileInputStream in = new FileInputStream(srcFile);
                     zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
            
                     while ((len = in.read(buf)) > 0) {
                           zip.write(buf, 0, len);
                     }
                }
              }
            
            
              static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws Exception {
            
                   File folder = new File(srcFolder);
                   String[] fileName = folder.list(); 
            
                   for (int i = 0; i < fileName.length; i++) {
            
                       if (path.equals("")) {
                          addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip);
            }
            else {
            addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName[i], zip);
            }
            }
            }
            }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
            • 3. Re: Zipping folders recursively
              800308
              Much better ;-)

              Ok What's it not doing... not adding folders to the Zip? So add a zipEntry for each folder (exactly as you add a file)... just be sure to add the directory before any files in that directory, to avert a bug in some unzippers which make the path for each file (if it doesn't already exist) but fail when they encounter a target folder which already exists.

              Cheers. Keith.
              • 4. Re: Zipping folders recursively
                843785
                Thank u for the advice!
                The program adds the folders to the zip file, but ignores (doesn't create) the EMPTY FOLDERS.I don't understand why and what am I missing :-)