0

I have a created a directory with some images stored in it. Now, to zip it as a single .zip file, I used the following code :

private static void zipDir(String zipFileName, String dir) throws Exception {
    File dirObj = new File(dir);
    ZipOutputStream out = new ZipOutputStream(newFileOutputStream(zipFileName));
    addDir(dirObj, out);
    out.close();
}

static void addDir(File dirObj, ZipOutputStream out) throws IOException {
    File[] files = dirObj.listFiles();
    byte[] tmpBuf = new byte[1024];

    for (int i = 0; i < files.length; i++) {
      if (files[i].isDirectory()) {
        addDir(files[i], out);
        continue;
     }
     FileInputStream in = new FileInputStream(files[i].getAbsolutePath());
     System.out.println(" Adding: " + files[i].getAbsolutePath());
      out.putNextEntry(new ZipEntry(files[i].getAbsolutePath()));
      int len;
      while ((len = in.read(tmpBuf)) > 0) {
        out.write(tmpBuf, 0, len);
      }
      out.closeEntry();
      in.close();
    }
  }

I obtained this code from the following source : http://www.java2s.com/Code/Java/File-Input-Output/Makingazipfileofdirectoryincludingitssubdirectoriesrecursively.htm

When I run this code, in the specified directory, a .zip file is created with the specified name but when I try to open it using any software(winzip, etc) on Android or on PC, it displays the error message that : This file is corrupt or not a valid zip file"

Any help would be appreciated.

Surya Prakash Kushawah
  • 3,185
  • 1
  • 22
  • 42
Siddharth Sharma
  • 310
  • 1
  • 2
  • 11
  • I think ZipEntry should be a file name, not a path. Try this: new ZipEntry(files[i].substring(files[i].lastIndexOf("/") + 1)); – Hahn Dec 08 '16 at 17:29

2 Answers2

0

I was thinking about deleting the question. But it stands for a rather interesting read. While creating the .zip file, I had specified the same directory as the one which I wanted to compress. This results in an infinite loop. Changing the .zip directory solves the issue.

Siddharth Sharma
  • 310
  • 1
  • 2
  • 11
0

I mean like this:

private static void zipDir(String zipFileName, String dir) throws Exception {
    List<String> files = buildFileList(dir, "");
    ZipOutputStream out = new ZipOutputStream(newFileOutputStream(zipFileName));
    zipFiles(files, out);
    out.close();
}

static List<String> buildFileList(String path, String filter) {
    List<String> lstFile = new ArrayList<String>();
    File[] files = new File(path).listFiles();

    if (files != null) {
        for (int i = 0; i < files.length; i++) {
            File f = files[i];
            if (f.isFile()) {
                if (filter.length() == 0 || f.getName().matches(filter))
                    lstFile.add(f.getAbsolutePath());

            } else if (f.isDirectory() && f.getPath().indexOf("/.") == -1)
                lstFile.addAll(getFilePaths(f.getAbsolutePath(), filter));
        }
    }
    return lstFile;
}

static void zipFiles(List<String> files, ZipOutputStream out) throws IOException {
    byte[] tmpBuf = new byte[1024];

    for (String file : files) {
      if (new File(file).isDirectory()) {
        continue;
     }
     FileInputStream in = new FileInputStream(file);
     System.out.println(" Adding: " + file);
      out.putNextEntry(new ZipEntry(file));
      int len;
      while ((len = in.read(tmpBuf)) > 0) {
        out.write(tmpBuf, 0, len);
      }
      out.closeEntry();
      in.close();
    }
}
reker
  • 2,063
  • 13
  • 12