在 Java 中解压导致“java.util.zip.ZipException"的 zip 文件;- 无效的 LOC 标头(错误的签名) [英] Unpacking zip files in Java that cause a "java.util.zip.ZipException" - invalid LOC header (bad signature)

查看:72
本文介绍了在 Java 中解压导致“java.util.zip.ZipException"的 zip 文件;- 无效的 LOC 标头(错误的签名)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 zip 文件(从 ownCloud 实例下载)并且无法使用 Java 库(Java 版本1.8.0_66")解压) 来自 java.util.zip.如果我尝试这样做,则会引发以下异常:

I have a zip file (that is downloaded from an ownCloud instance) and can not be unpacked using the Java libraries (java version "1.8.0_66") from java.util.zip. If I try to do so, the following exception is thrown:

java.util.zip.ZipException:无效的 LOC 标头(错误签名)

当发出 Linux 'file' 命令时,它输出以下内容:

When issuing a Linux 'file' command, it outputs the following:

so-example.zip:压缩存档数据,至少要解压 v3.0

经过反复试验后我发现,那些应该可以通过v2.0"解压缩的文件,可以被 Java 处理而没有任何问题.

After some trial and error I found out, that files, that are supposed to be unzippable by "v2.0", can be processed by Java without any issues.

用于解包的代码的最小示例如下:

A minimal example for the code used for unpacking is the following:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class PlaygroundZip{
    public static void main(String[] args) {
        String filename = "/tmp/so-example.zip";
        byte[] buffer = new byte[1024];

        try {
            ZipFile zipFile = new ZipFile(filename);
            Enumeration entries = zipFile.entries();
            while(entries.hasMoreElements()) {
                ZipEntry entry = (ZipEntry)entries.nextElement();
                String currName = entry.getName();

                System.out.println("File: " + currName);

                if(entry.isDirectory()) {
                    new File(currName).mkdirs();
                } else{
                    InputStream zis = zipFile.getInputStream(entry);
                    FileOutputStream fos = new FileOutputStream(currName);             

                    int len;
                    while ((len = zis.read(buffer)) > 0) {
                        fos.write(buffer, 0, len);
                    }
                    fos.close();  
                }
            }
            zipFile.close();
        } catch (IOException ioe) {
            System.err.println("Unhandled exception:");
            ioe.printStackTrace();
        }

    }
}

如何使用标准 Java 库或任何其他方法处理这些文件?

How can I process these files with standard Java libraries or any other method?

推荐答案

到目前为止,我发现解压这些文件的唯一方法是使用系统命令并使用unzip"命令行工具(Ubuntu 12.04 LTS,解压缩 6.00).由于这是在服务器应用程序中使用的,因此它是一种可以接受的 hack,即使远非优雅.

The only way to unpack those files, I have found so far, is using a system command and use the "unzip" command line tool (Ubuntu 12.04 LTS, UnZip 6.00). As this is used in a server application it would be an acceptable hack, even though far away from being elegant.

Process p = Runtime.getRuntime().exec("unzip " + filename + " -d /tmp/mydir");

这篇关于在 Java 中解压导致“java.util.zip.ZipException"的 zip 文件;- 无效的 LOC 标头(错误的签名)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆