unzip archive with subfolders in java?

2019-06-14 07:56发布

问题:

I am trying to unzip an archive (test.zip) containing a subfolder with some png images:

test.zip
  | -> images
         | -> a.png
         | -> b.png

Here is what I do:

  public static void unzip(String archive, File baseFolder, String[] ignoreExtensions) {
    FileInputStream fin;
    try {
      fin = new FileInputStream(archive);
      ZipInputStream zin = new ZipInputStream(fin);
      ZipEntry ze = null;
      while ((ze = zin.getNextEntry()) != null) {
        if (ignoreExtensions == null || !ignoreEntry(ze, ignoreExtensions)) {
          File destinationFile = new File(baseFolder, ze.getName());
          unpackEntry(destinationFile, zin);
        }
      }
      zin.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  private static void unpackEntry(File destinationFile, ZipInputStream zin) {
    createParentFolder(destinationFile);
    FileOutputStream fout = null;
    try {
      fout = new FileOutputStream(destinationFile);
      for (int c = zin.read(); c != -1; c = zin.read()) {
        fout.write(c);
        zin.closeEntry();
        fout.close();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }

  }

  private static void createParentFolder(File destinationFile) {
    File parent = new File(destinationFile.getParent());
    parent.mkdirs();
  }

The images are extracted to the correct location but are corrupt (the size is smaller than expected so I assume they are not decompressed).

If I open the test.zip file with 7Zip it works fine. Any ideas on how to unzip an archive with subfolders?

回答1:

What are you doing here?

  for (int c = zin.read(); c != -1; c = zin.read()) {
    fout.write(c);
    zin.closeEntry();
    fout.close();
  }

Could it be that you meant this instead?

  for (int c = zin.read(); c != -1; c = zin.read()) {
    fout.write(c);
  }
  zin.closeEntry();
  fout.close();


回答2:

It can be done as below by checking whether the unzipped entry is a directory. If directory then create the directory and proceed with streaming the file inside the directory.

private void unZipFile(long lBatchID, String sFileName) throws Exception {
    final int BUFFER = 2048;
    BufferedOutputStream dest = null;
    FileInputStream fis = null;
    ZipInputStream zis = null;
    int iSubstr1 = sFileName.indexOf("-");
    int iSubstr2 = sFileName.lastIndexOf("-");
    int iEDocketSubStr = sFileName.lastIndexOf("\\");
    String sBatchNum = sFileName.substring(iSubstr1 + 1,
            iSubstr2);
    String sEDocketNum = sFileName.substring(iEDocketSubStr + 1,
            iSubstr1);
    Date startTime = new Date();
    try {

        fis = new FileInputStream(sFileName);
        zis = new ZipInputStream(
                new BufferedInputStream(fis));
        ZipEntry entry;
        String sTempDir = TEMP_DIR + "\\" + sEDocketNum+"-"+sBatchNum;
        File fTempDir = new File(sTempDir);
        fTempDir.mkdirs();
        while ((entry = zis.getNextEntry()) != null) {
            int count;
            byte data[] = new byte[BUFFER];
            if(entry.isDirectory())
            {
                File f2 = new File(TEMP_DIR + "\\" + sEDocketNum+"-"+sBatchNum+"\\"+entry.getName());
                f2.mkdir();
                logger.debug("Creating directory during unzip....."+entry.getName());
            }
            else
            {
            FileOutputStream fos = new FileOutputStream(new File(sTempDir
                    + "\\" + entry.getName()));
            dest = new BufferedOutputStream(fos, BUFFER);
            while ((count = zis.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
            }
        }
        zis.close();
        LogTaskDuration.logDuration(lBatchID, startTime, "UNZIP");
    } catch (Exception e) {

        e.printStackTrace();
        logger.error("Problem unzipping file - " + sFileName);
        throw new Exception(
                "Could not create temporary directory to unzip file");
    }
    finally
    {
        if(dest != null)
            dest.close();
        if(fis!=null)
            fis.close();

    }
}