【问题标题】:Empty Folder when try to unzip using java.util.zip / zip4j尝试使用 java.util.zip / zip4j 解压缩时的空文件夹
【发布时间】:2016-11-15 12:02:30
【问题描述】:

我收到了一个 zip 扩展名的压缩文件。 我无法使用 Windows 资源管理器直接打开它。 我可以使用 7Zip 提取它,它会引发一些错误,但文件仍按预期解压缩。 我可以使用winrar解压,没有错误,文件按预期解压。

然后我尝试使用 java.util.unzip / zip4j 解压缩。

java.util.zip 代码:

    public static void unzip(String zipFilePath,
                         String destDirectory) throws IOException {
    File destDir = new File(destDirectory);
    if (!destDir.exists()) {
        destDir.mkdir();
    }
    ZipInputStream zipIn =
        new ZipInputStream(new FileInputStream(zipFilePath));
    ZipEntry entry = zipIn.getNextEntry();
    // iterates over entries in the zip file
    while (entry != null) {
        String filePath = destDirectory + File.separator + entry.getName();
        if (!entry.isDirectory()) {
            // if the entry is a file, extracts it
            extractFile(zipIn, filePath);
        } else {
            // if the entry is a directory, make the directory
            File dir = new File(filePath);
            dir.mkdir();
        }
        zipIn.closeEntry();
        entry = zipIn.getNextEntry();
    }
    zipIn.close();
}

/**
 * Extracts a zip entry (file entry)
 * @param zipIn
 * @param filePath
 * @throws IOException
 */
private static void extractFile(ZipInputStream zipIn,
                                String filePath) throws IOException {
    BufferedOutputStream bos =
        new BufferedOutputStream(new FileOutputStream(filePath));
    byte[] bytesIn = new byte[BUFFER_SIZE];
    int read = 0;
    while ((read = zipIn.read(bytesIn)) != -1) {
        bos.write(bytesIn, 0, read);
    }
    bos.close();
}

使用上面的代码,没有错误发生,但我得到一个空文件夹。

zip4j 代码:

        String zipFilePath = "C:\\Juw\\JR\\file\\output\\020030214112016.zip";
    //String zipFilePath = "C:\\Juw\\JR\\file\\output\\myFile.zip";
    String destDirectory = "C:\\Juw\\JR\\file\\output\\targetUnzip";

    try {
        ZipFile zipFile = new ZipFile(zipFilePath);
        zipFile.extractAll(destDirectory);

    } catch (Exception ex) {
        ex.printStackTrace();
    }

还有一个例外: net.lingala.zip4j.exception.ZipException:未找到 zip 标头。可能不是 zip 文件

如果我尝试使用 winrar 解压缩文件,然后我使用 Windows 内置的 zip 功能对其进行压缩。我可以使用我的代码成功解压缩它。我的尺寸和客户给我的尺寸不同。我的是508kb,另一个是649kb。

问题是: - 是否有任何使用 / 作为强大的 winrar 的 java 库,可以无错误地提取压缩文件? - 解决这种情况的最佳做法是什么?

提前非常感谢:)

【问题讨论】:

  • "解决此案例的最佳做法是什么?" - 要求客户提供有效的 .zip 文件。
  • 嗨 JimmyB,您是否怀疑 zip 文件无效?因为winrar可以毫无问题地提取它。客户端是否有可能使用具有不同标准 zip 的 3rd 方应用程序对其进行压缩?或来自其他操作系统(不是 Windows)?
  • 如果 Windows 和 7zip 以及 Java 库失败或报告错误,我认为文件可能在某种程度上不完全有效。也许它甚至不是 zip 文件,而是写入名为 .zip 的文件的其他格式。不依赖文件名来确定文件实际具有什么格式的工具可能仍然能够解压缩它。

标签: java file zip compression zip4j


【解决方案1】:

感谢所有帮助我的受访者。

所以情况是这个 gzip 文件(命名为 .zip 扩展名)在文件末尾有一个空字符串。所以它抛出了我在上一篇文章中提到的错误。使用我从同事那里得到的以下代码,问题就解决了。我发布它,以便其他用户将来可以使用它。

public void gunzipIt(){

 byte[] buffer = new byte[1024];
 boolean isValid = true;

 try{

     GZIPInputStream gzis =
        new GZIPInputStream(new FileInputStream(INPUT_GZIP_FILE));

     FileOutputStream out =
        new FileOutputStream(OUTPUT_FILE);

    while (isValid) {

        int len;

        try{
            len = gzis.read(buffer);
        }catch(Exception ex){
            len = 0;
            isValid = false;
        }

        if (len > 0) {
            out.write(buffer, 0, len);
        }else{

            isValid = false;
        }
    }

    gzis.close();
    out.close();

    System.out.println("Done");

}catch(IOException ex){
   ex.printStackTrace();
}

}

【讨论】:

    最近更新 更多