I want to open a ZIP-file, that have no entries with java.util.zip.ZipFile. But on the constructor I get the following exception: 'java.util.zip.ZipException: error in opening zip file'. How can I open the empty ZIP?
我想打开一个ZIP文件,它没有java.util.zip.ZipFile的条目。但在构造函数上,我得到以下异常:'java.util.zip.ZipException:打开zip文件时出错'。如何打开空ZIP?
That ZIP-file is created by the commandline zip-program under linux. I simply deleted all entries from a ZIP-file.
该ZIP文件由linux下的命令行zip程序创建。我只是删除了ZIP文件中的所有条目。
I need this as testdata for a class I write. The class should simply return an empty list for this case, but broken ZIP-files should return an error.
我需要这个作为我写的类的testdata。该类应该只为这种情况返回一个空列表,但是破损的ZIP文件应该返回错误。
For some more explanation on the problem. I have an interface, for extracting some documents from different sources. Other implementations gather them from webservices or directories, this implementation from ZIP-files. The interface give an Iterator with some more functionality. So I want to decide, if the ZIP-file is empty or broken.
有关该问题的更多解释。我有一个界面,用于从不同的来源提取一些文件。其他实现从webservices或目录收集它们,这个实现来自ZIP文件。该接口为迭代器提供了更多功能。所以我想决定,如果ZIP文件是空的还是坏了。
7 个解决方案
#1
5
hack: you can assume that all empty ZIPs are the same and just hardcode it's length/chechsum to validate against.
hack:你可以假设所有空的ZIP都是相同的,只需硬编码它的长度/ chechsum来验证。
#2
2
My solution for this problem is now, that I simply use ZipInputStream instead of ZipFile. This class works well with empty ZIP-files. I don't know about the reason, why one works and the other not.
我现在解决这个问题的方法是,我只使用ZipInputStream而不是ZipFile。这个类适用于空的ZIP文件。我不知道原因,为什么一个有效,另一个没有。
#3
1
I don't know why is it implemented this way, but why do you need to succesfully open an empty Zip file? You can't modify it with java.util.zip.ZipFile anyway...
我不知道为什么这样实现,但为什么你需要成功打开一个空的Zip文件?你不能用java.util.zip.ZipFile修改它...
So you can just catch the ZipException (which is thrown for invalid format zip files), and skip the file if you catch it.
所以你可以捕获ZipException(对于无效的格式zip文件抛出),如果你抓住它就跳过该文件。
#4
1
I think the reason ZipInputStream works and ZipFile does not is because of the two different ways that zip files are read. The ZipFile constructor attempts to read the ZipFile's table of contents, which is written to the end of the file. If it can't read the TOC, it throws a ZipException (with almost no helpful info contained therein), which I think is what you're seeing. ZipInputStream, however, reads the entries out of the zip file sequentially starting at the beginning of the file, so it seems more robust in this case.
我认为ZipInputStream工作的原因和ZipFile不是因为读取zip文件的两种不同方式。 ZipFile构造函数尝试读取ZipFile的目录,该目录写入文件的末尾。如果它无法读取TOC,它会抛出一个ZipException(其中几乎没有包含有用的信息),我认为这就是你所看到的。但是,ZipInputStream从文件的开头依次读取zip文件中的条目,因此在这种情况下看起来更健壮。
This is all very poorly documented and I've run into similar problems myself using ZipFile. Both methods of reading from a zip file are valid, but you'd think the API docs would mention the implications of the random access/TOC method of reading through constructor versus reading through a ZipInputStream.
这些都是非常糟糕的文档,我自己使用ZipFile遇到了类似的问题。从zip文件中读取这两种方法都是有效的,但您认为API文档会提到随机访问/ TOC方法通过构造函数读取而不是通过ZipInputStream读取的含义。
#5
0
Are you sure it is a valid zip file? That would be my first guess.
你确定它是一个有效的zip文件吗?这将是我的第一个猜测。
#7
-1
Use a ZipOutputStream
.
使用ZipOutputStream。
#1
5
hack: you can assume that all empty ZIPs are the same and just hardcode it's length/chechsum to validate against.
hack:你可以假设所有空的ZIP都是相同的,只需硬编码它的长度/ chechsum来验证。
#2
2
My solution for this problem is now, that I simply use ZipInputStream instead of ZipFile. This class works well with empty ZIP-files. I don't know about the reason, why one works and the other not.
我现在解决这个问题的方法是,我只使用ZipInputStream而不是ZipFile。这个类适用于空的ZIP文件。我不知道原因,为什么一个有效,另一个没有。
#3
1
I don't know why is it implemented this way, but why do you need to succesfully open an empty Zip file? You can't modify it with java.util.zip.ZipFile anyway...
我不知道为什么这样实现,但为什么你需要成功打开一个空的Zip文件?你不能用java.util.zip.ZipFile修改它...
So you can just catch the ZipException (which is thrown for invalid format zip files), and skip the file if you catch it.
所以你可以捕获ZipException(对于无效的格式zip文件抛出),如果你抓住它就跳过该文件。
#4
1
I think the reason ZipInputStream works and ZipFile does not is because of the two different ways that zip files are read. The ZipFile constructor attempts to read the ZipFile's table of contents, which is written to the end of the file. If it can't read the TOC, it throws a ZipException (with almost no helpful info contained therein), which I think is what you're seeing. ZipInputStream, however, reads the entries out of the zip file sequentially starting at the beginning of the file, so it seems more robust in this case.
我认为ZipInputStream工作的原因和ZipFile不是因为读取zip文件的两种不同方式。 ZipFile构造函数尝试读取ZipFile的目录,该目录写入文件的末尾。如果它无法读取TOC,它会抛出一个ZipException(其中几乎没有包含有用的信息),我认为这就是你所看到的。但是,ZipInputStream从文件的开头依次读取zip文件中的条目,因此在这种情况下看起来更健壮。
This is all very poorly documented and I've run into similar problems myself using ZipFile. Both methods of reading from a zip file are valid, but you'd think the API docs would mention the implications of the random access/TOC method of reading through constructor versus reading through a ZipInputStream.
这些都是非常糟糕的文档,我自己使用ZipFile遇到了类似的问题。从zip文件中读取这两种方法都是有效的,但您认为API文档会提到随机访问/ TOC方法通过构造函数读取而不是通过ZipInputStream读取的含义。
#5
0
Are you sure it is a valid zip file? That would be my first guess.
你确定它是一个有效的zip文件吗?这将是我的第一个猜测。
#6
#7
-1
Use a ZipOutputStream
.
使用ZipOutputStream。