Java解压文件时出现ZipException

Hei*_*Hei 5 java zip

我对使用 Java 处理 zip 文件完全陌生,\n并且我遇到了一种奇怪的情况。

\n\n

这是我用于解压的方法:

\n\n
public void unzip(File zipFile, File rootDir) throws IOException\n{\n    ZipFile zip = new ZipFile(zipFile);\n    Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries();\n\n    while(entries.hasMoreElements()) {\n        ZipEntry entry = entries.nextElement();\n        java.io.File f = new java.io.File(rootDir, entry.getName());\n        if (entry.isDirectory()) { // if its a directory, create it\n            continue;\n        }\n\n        if (!f.exists()) {\n            f.getParentFile().mkdirs();\n            f.createNewFile();\n        }\n\n        /*BufferedInputStream bis = new BufferedInputStream(zip.getInputStream(entry)); // get the input stream\n        BufferedOutputStream bos = new BufferedOutputStream(new java.io.FileOutputStream(f));\n        while (bis.available() > 0) {  // write contents of \'is\' to \'fos\'\n            bos.write(bis.read());\n        }\n        bos.close();\n        bis.close();*/\n\n        InputStream is = zip.getInputStream(entry);\n        OutputStream os = new java.io.FileOutputStream(f);\n        byte[] buf = new byte[4096];\n        int r ;\n        while ((r = is.read(buf)) != -1) {\n            os.write(buf, 0, r);\n        }\n        os.close();\n        is.close();\n    }   \n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

但是,已引发 IOException,消息为:

\n\n

信息| JVM 1 | 2012/11/30 01:58:05 | java.util.zip.ZipException:打开 zip 文件时出错

\n\n

信息| JVM 1 | 2012/11/30 01:58:05 | 在 java.util.zip.ZipFile.open(本机方法)

\n\n

信息| JVM 1 | 2012/11/30 01:58:05 | 在 java.util.zip.ZipFile.(ZipFile.java:127)

\n\n

信息| JVM 1 | 2012/11/30 01:58:05 | 在 java.util.zip.ZipFile.(ZipFile.java:143)

\n\n

有人能帮我解决这个问题吗?

\n\n

多谢。

\n\n

更新:

\n\n

我使用的是Linux作为测试环境。\n解压目录的权限是drwxr-xr-x \xe2\x80\x93

\n\n

更新02:

\n\n

通过采纳@heikkim 的建议,

\n\n

我刚刚尝试在 Linux 中使用 unzip 命令,尝试手动解压缩我的文件。我有以下消息:

\n\n

存档:TMA_Template.zip\n注意:zipfile 注释被截断\n警告 [TMA_Template.zip]:zipfile 声称是多部分存档的最后一个磁盘;\n 无论如何尝试处理,假设所有部分已按顺序连接\n 在一起。预计会出现“错误”和警告...真正的多部分支持\n 尚不存在(即将推出)。\n错误 [TMA_Template.zip]:zip 文件中缺少 6366880279 字节\n(无论如何都在尝试处理)\n错误[TMA_Template.zip]:尝试在 zip 文件开头之前查找\n(请检查您是否已以适当的 BINARY 模式传输或创建了 zip 文件,并且已正确编译 UnZip)

\n

Per*_*eel 0

你可以尝试用这个方法吗:

private void unzip() throws IOException {
    int BUFFER = 2048;
    BufferedOutputStream dest = null;
    BufferedInputStream is = null;
    ZipEntry entry;
    ZipFile zipfile = new ZipFile("latest.zip");
    Enumeration e = zipfile.entries();
    (new File(root)).mkdir();
    while (e.hasMoreElements()) {
        entry = (ZipEntry) e.nextElement();
        //outText.setText(outText.getText() + "\nExtracting: " + entry);
        if (entry.isDirectory()) {
            (new File(root + entry.getName())).mkdir();
        } else {
            (new File(root + entry.getName())).createNewFile();
            is = new BufferedInputStream(zipfile.getInputStream(entry));
            int count;
            byte data[] = new byte[BUFFER];
            FileOutputStream fos = new FileOutputStream(root + entry.getName());
            dest = new BufferedOutputStream(fos, BUFFER);
            while ((count = is.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
            is.close();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)