ZipFile 抛出错误,但 ZipInputStream 能够解压缩存档 [英] ZipFile is throwing error but ZipInputStream is able to decompress the archive

查看:23
本文介绍了ZipFile 抛出错误,但 ZipInputStream 能够解压缩存档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 java.util.zip.*

我有一个 zip 文件,解压后会发生以下情况

I have a zip file and upon decompressing follwing tihngs happen

ZipFile zipfile = new ZipFile(file, ZipFile.OPEN_READ);

这是exaxt错误信息

This is exaxt error message

java.util.zip.ZipException: error in opening zip file
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:127)
at java.util.zip.ZipFile.<init>(ZipFile.java:143)
at com.basware.ExtractZip.unpack(ExtractZip.java:27)
at com.basware.ExtractZip.main(ExtractZip.java:17)

但是如果我使用以下代码,它可以毫无错误地打开存档

But if I use the following code it is able to open the archive without any errors

try {
     BufferedOutputStream dest = null;       
     File file = new File("File_Path");        
     FileInputStream fis = new FileInputStream(file);
     ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
     ZipEntry entry;
     while((entry = zis.getNextEntry()) != null) {
        System.out.println("Extracting: " +entry);
        int count;
        byte data[] = new byte[BUFFER];
        // write the files to the disk
        FileOutputStream fos = new 
      FileOutputStream(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();

请注意,文件是使用 WinZIP 压缩的.

Please note that files are compressed using WinZIP.

我的问题是 ZipFile 和 ZipInputStream 几乎相同,为什么 ZipFile 给出异常以及为什么它无法执行解压缩.

My question is as ZipFile and ZipInputStream are almost same ,why ZipFile is giving exception and why it is unable to perform decompression.

问题是如果我使用 WinZip 工具压缩文件,然后使用列出的程序解压缩它,它工作正常.但是,这个问题专门针对来自外部来源的档案(外部来源声称他们正在使用 WinZip). 最重要的是,如果我使用 WinZip 工具打开相同的存档(外部存档),它会显示和解压缩文件.但是这个 JAVA 特定代码(ZipFile)根本不起作用.

EDIT : The problem is if I zip the file using WinZip tool and then decompress it using listed program it is working fine.But, this problem is specifically coming for archives coming from external source(external source claims that they are using WinZip).On top of it, if I open the very same archive(external one) using WinZip tool it is showing and decompressing files.But this JAVA specific code(ZipFile) is not working at all.

我无法弄清楚为什么 java 本机代码不适用于我的 ZIP 档案,但是 apache compress 解决了我的问题.它按照 Ian Roberts 的建议对我有用.

I am not able to figure it out why java native code is not working for my ZIP archives, but apache compress solved my problem.It is working for me as suggested by Ian Roberts.

推荐答案

ZipFile 尝试解析 zip 末尾的中央目录",以构建一个数据结构,允许您按名称访问单个条目.ZipInputStream 没有,它只在从上到下读取文件时查看每个条目的本地标头.因此,您的文件似乎具有良好的条目,但由于某种原因中央目录已损坏.

ZipFile attempts to parse the "central directory" at the end of the zip in order to build up a data structure that allows you to access individual entries by name. ZipInputStream doesn't, it only looks at the local header of each entry as it reads through the file from top to bottom. So it looks like your file has good entries but a corrupted central directory for some reason.

有多种可能性,例如 非编码的问题条目名称中的 ASCII 字符,或者 zip 的条目超过 64k.我会尝试 ZipFile 的 commons-compress 实现 - 即使它不起作用它也应该给你比从 java.util.zip 获得的出现问题"更具体的错误消息.

There are a number of possibilities, for example issues with the encoding of non-ASCII characters in entry names, or if the zip has more than 64k entries. I would try the commons-compress implementation of ZipFile - even if it doesn't work it should give you a more specific error message than the "something is wrong" that you get from java.util.zip.

这篇关于ZipFile 抛出错误,但 ZipInputStream 能够解压缩存档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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