0

我正在尝试解压缩包含带有一些 png 图像的子文件夹的存档(test.zip):

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

这是我所做的:

  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();
  }

图像被提取到正确的位置,但已损坏(大小小于预期,因此我假设它们未解压缩)。

如果我用 7Zip 打开 test.zip 文件,它可以正常工作。关于如何解压缩带有子文件夹的存档的任何想法?

4

2 回答 2

2

你在这里做什么?

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

难道你的意思是这个吗?

  for (int c = zin.read(); c != -1; c = zin.read()) {
    fout.write(c);
  }
  zin.closeEntry();
  fout.close();
于 2011-06-15T21:40:46.830 回答
0

可以通过检查解压缩的条目是否为目录来完成如下。如果是目录,则创建目录并继续在目录中流式传输文件。

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();

    }
}
于 2011-06-21T07:40:05.967 回答