在Java中解压缩文件时的ZipException

时间:2012-11-30 09:36:59

标签: java zip

我对使用Java处理zip文件感到非常新鲜, 我遇到了一个奇怪的情况。

以下是我用于解压缩的方法:

public void unzip(File zipFile, File rootDir) throws IOException
{
    ZipFile zip = new ZipFile(zipFile);
    Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries();

    while(entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        java.io.File f = new java.io.File(rootDir, entry.getName());
        if (entry.isDirectory()) { // if its a directory, create it
            continue;
        }

        if (!f.exists()) {
            f.getParentFile().mkdirs();
            f.createNewFile();
        }

        /*BufferedInputStream bis = new BufferedInputStream(zip.getInputStream(entry)); // get the input stream
        BufferedOutputStream bos = new BufferedOutputStream(new java.io.FileOutputStream(f));
        while (bis.available() > 0) {  // write contents of 'is' to 'fos'
            bos.write(bis.read());
        }
        bos.close();
        bis.close();*/

        InputStream is = zip.getInputStream(entry);
        OutputStream os = new java.io.FileOutputStream(f);
        byte[] buf = new byte[4096];
        int r ;
        while ((r = is.read(buf)) != -1) {
            os.write(buf, 0, r);
        }
        os.close();
        is.close();
    }   
}

但是,抛出了IOException并且消息为:

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

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

INFO | jvm 1 | 2012/11/30 01:58:05 |在java.util.zip.ZipFile。(ZipFile.java:127)

INFO | jvm 1 | 2012/11/30 01:58:05 |在java.util.zip.ZipFile。(ZipFile.java:143)

有人可以帮我吗?

非常感谢。

更新

我使用Linux作为测试环境。 解压缩目录的权限是drwxr-xr-x -

更新02:

采纳@heikkim的建议,

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

存档:TMA_Template.zip 警告:zipfile注释被截断 警告[TMA_Template.zip]:zipfile声称是多部分存档的最后一个磁盘;   无论如何都要尝试处理,假设所有部分都已连接在一起   一起按顺序。期待“错误”和警告......真正的多部分支持   尚不存在(即将推出)。 错误[TMA_Template.zip]:zipfile中缺少6366880279个字节   (无论如何都试图处理) 错误[TMA_Template.zip]:尝试在开始zipfile之前进行搜索   (请检查您是否已转移或创建了zip文件   适当的BINARY模式,你已经正确编译了UnZip)

1 个答案:

答案 0 :(得分: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();
        }
    }
}
相关问题