2 Replies Latest reply: Jul 25, 2009 5:32 PM by 807588 RSS

    Unable to open a Zip directory using windows default uncomressed folder

    807588
      Hi
      I Created a zip directory in AIX using java.util.zipfile. now when I am trying to open it in windows using its defult uncompress method I am unable to do it.

      here is the sample code

      import java.io.File;
      import java.io.FileInputStream;
      import java.io.FileNotFoundException;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.util.zip.ZipEntry;
      import java.util.zip.ZipOutputStream;

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

      fileWriter = new FileOutputStream(destZipFile);
      zip = new ZipOutputStream(fileWriter);
      addFolderToZip(Z_PATH, srcFolder, zip);
      zip.flush();
      zip.close();
      }

      public void addFileToZip(String path, String srcFile, ZipOutputStream zip)
      {
      System.out.println("file in addFileToZip " + srcFile);
      try {
                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(srcFile ));
                while ((len = in.read(buf)) > 0) {
                zip.write(buf, 0, len);
                }
                /*}*/
           } catch (FileNotFoundException e) {
                
                e.printStackTrace();
           } catch (IOException e) {
                
                e.printStackTrace();
           } catch (Exception e) {
                
                e.printStackTrace();
           }
      }

      public void addFolderToZip(String Z_PATH, String srcFolder, ZipOutputStream zip)
      throws Exception {
      File folder = new File(srcFolder);
      System.out.println("Folder Name::"+folder.getName());
      for (String fileName : folder.list()) {
      //if (path.equals("")) {
           System.out.println("Filename to be zipped" + fileName);
      addFileToZip(Z_PATH, srcFolder + fileName, zip);
      //} else {
      //addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip);
      //}
      }
      }
      }

      the srcFolder referes to /a/b/
      destZipfile refres to /a/b.zip
      srcfile refers to /a/b/c.txt

      now the zip gets created but it takes the path /a/b.Due to which I m unable to open it in windows.

      please help me in removing the / from the Zip file.
        • 1. Re: Unable to open a Zip directory using windows default uncomressed folder
          807588
          Hello and welcome to the forum. Let me add code tags to your posted code
          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileNotFoundException;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.util.zip.ZipEntry;
          import java.util.zip.ZipOutputStream;
          
          public class Zipdita {
             
                
           public void zipFolder(String srcFolder, String destZipFile, String Z_PATH) throws Exception {
              ZipOutputStream zip = null;
              FileOutputStream fileWriter = null;
          
              fileWriter = new FileOutputStream(destZipFile);
              zip = new ZipOutputStream(fileWriter);
              addFolderToZip(Z_PATH, srcFolder, zip);
              zip.flush();
              zip.close();
            }
          
            public void addFileToZip(String path, String srcFile, ZipOutputStream zip)
                 {
          System.out.println("file in addFileToZip " + srcFile);
              try {
                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(srcFile ));
                  while ((len = in.read(buf)) 0) {
                    zip.write(buf, 0, len);
                  }
                /*}*/
             } catch (FileNotFoundException e) {
                
                e.printStackTrace();
             } catch (IOException e) {
                
                e.printStackTrace();
             } catch (Exception e) {
                
                e.printStackTrace();
             }
            }
          
          public void addFolderToZip(String Z_PATH, String srcFolder, ZipOutputStream zip)
                throws Exception {
              File folder = new File(srcFolder);
              System.out.println("Folder Name::"+folder.getName());
              for (String fileName : folder.list()) {
                //if (path.equals("")) {
                 System.out.println("Filename to be zipped" + fileName);
                  addFileToZip(Z_PATH, srcFolder + fileName, zip);
                //} else {
                  //addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip);
                //}
              }
            }
          }
          • 2. Re: Unable to open a Zip directory using windows default uncomressed folder
            807588
            how can i remove the "/" from my zip path?