如何检查生成的 zip 文件是否已损坏?
我们有一段代码可以在我们的系统上生成一个 zip 文件。一切正常,但有时该 zip 文件在由 FilZip 或 WinZip 打开时被认为已损坏。
所以这是我的问题:我们如何以编程方式检查生成的 zip 文件是否已损坏?
这是我们用来生成 zip 文件的代码:
try {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(tmpFile));
byte[] buffer = new byte[16384];
int contador = -1;
for (DigitalFile digitalFile : document.getDigitalFiles().getContent()) {
ZipEntry entry = new ZipEntry(digitalFile.getName());
FileInputStream fis = new FileInputStream(digitalFile.getFile());
try {
zos.putNextEntry(entry);
while ((counter = fis.read(buffer)) != -1) {
zos.write(buffer, 0, counter);
}
fis.close();
zos.closeEntry();
} catch (IOException ex) {
throw new OurException("It was not possible to read this file " + arquivo.getId());
}
}
try {
zos.close();
} catch (IOException ex) {
throw new OurException("We couldn't close this stream", ex);
}
我们在这里做错了什么吗?
编辑: 其实上面的代码是完全没问题的。我的问题是我为我的用户重定向了错误的流。因此,他们不是打开 zip 文件,而是打开完全不同的文件。我很抱歉 :(
但主要问题仍然是:如何以编程方式验证给定的 zip 文件是否未损坏?
we have a piece of code which generates a zip file on our system. Everything is ok, but sometimes this zip file while opened by FilZip or WinZip is considered to be corrupted.
So here is my question: how can we check programatically if a generated zip file is corrupted?
Here is the code we are using to generate our zip files:
try {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(tmpFile));
byte[] buffer = new byte[16384];
int contador = -1;
for (DigitalFile digitalFile : document.getDigitalFiles().getContent()) {
ZipEntry entry = new ZipEntry(digitalFile.getName());
FileInputStream fis = new FileInputStream(digitalFile.getFile());
try {
zos.putNextEntry(entry);
while ((counter = fis.read(buffer)) != -1) {
zos.write(buffer, 0, counter);
}
fis.close();
zos.closeEntry();
} catch (IOException ex) {
throw new OurException("It was not possible to read this file " + arquivo.getId());
}
}
try {
zos.close();
} catch (IOException ex) {
throw new OurException("We couldn't close this stream", ex);
}
Is there anything we are doing wrong here?
EDIT:
Actually, the code above is absolutely ok. My problem was that I was redirecting the WRONG stream for my users. So, instead of opening a zip file they where opening something completely different. Mea culpa :(
BUT the main question remains: how programatically I can verify if a given zip file is not corrupted?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以使用 ZipFile 类来检查您的文件:
You can use the
ZipFile
class to check your file :我知道这个发布已经有一段时间了,我已经使用了你们所有人提供的代码并提出了这个。这对于实际问题非常有效。检查 zip 文件是否损坏
I know its been a while that this has been posted, I have used the code that all of you provided and came up with this. This is working great for the actual question. Checking if the zip file is corrupted or not
我想您会在 zip 文件生成过程中看到相应的异常堆栈跟踪。因此,您可能不想增强异常处理。
I think you'll see correspondent exception stack trace during zip-file generation. So, you probably wan't to enhance your exception handling.
在我的实现中它看起来像这样。也许它可以帮助你:
in my implementation it looks like that. maybe it helps you:
也许交换以下两行?
我可以想象 closeEntry() 仍然会从流中读取一些数据。
Perhaps swap the following two lines?;
I can imagine that the closeEntry() will still read some data from the stream.
您的代码基本上没问题,请尝试找出哪个文件导致损坏的 zip 文件。检查 digitalFile.getFile() 是否始终向 FileInputStream 返回有效且可访问的参数。只需在代码中添加一些日志记录,您就会发现问题所在。
Your code is basically OK, try to find out which file is responsible for the corrupted zip file. Check whether digitalFile.getFile() always returns a valid and accessible argument to FileInputStream. Just add a bit logging to your code and you will find out what's wrong.
再次压缩文件,因此重复工作,这不是您想要的。尽管只检查一个文件,但问题是压缩 n 个文件。
看看这个: http://www.kodejava.org/examples/336.html< /a>
为您的 zip 创建一个校验
和:当您完成压缩时显示它
您必须使用 java 或其他工具执行相同的读取 zip 操作,检查校验和是否匹配。
有关更多信息,请参阅 https://stackoverflow.com/a/10689488/848072
compress again the file, so duplicate efforts and that is not what you are looking for. Despite of the fact that only check one file and the question compress n-files.
Take a look to this: http://www.kodejava.org/examples/336.html
Create a checksum for your zip:
And when you finish the compression show it
You must do the same reading the zip with java or others tools checking if checksums match.
see https://stackoverflow.com/a/10689488/848072 for more information
ZipOutputStream 不关闭 底层流。
您需要做的是:
然后在结束块中:
ZipOutputStream does not close the underlying stream.
What you need to do is:
Then in your closing block:
我通过使用 try-with-resources 简化了 Nikhil Das Nomula 的代码:
起初我认为使用 ZipFile 是多余的,但后来我删除了末尾的一个字节存档文件。
ZipEntry
没有显示问题,但ZipFile
抛出错误java.util.zip.ZipException:未找到 zip END header
。I simplified the code from
Nikhil Das Nomula
by using try-with-resources:At first I thought using
ZipFile
was redundant, but then I deleted one byte at the end of the archive file.ZipEntry
did not show the problem, butZipFile
threw the errorjava.util.zip.ZipException: zip END header not found
.