[英]How to read the ZipEntry CRC from a nested archive (zip in zip)
See comments of selected answer, the issue was with my test data and not the code. 查看所选答案的注释,问题出在我的测试数据而不是代码。
I'm trying to read the CRCs of a Zip archive using the ZipEntry object. 我正在尝试使用ZipEntry对象读取Zip存档的CRC。 This works for zips directly on the file system. 这直接在文件系统上适用于zip。
zStream = new ZipInputStream(new FileInputStream(zipPath))
zEntry = zStream.getNextEntry()
zEntry.getCrc()
I want to get the CRC from a nested zipEntry, but I always returns -1 我想从嵌套的zipEntry获取CRC,但是我总是返回-1
zStream = new ZipInputStream(new ZipFile(zipPath).getInputStream(zEntry))
zStream.getNextEntry().getCrc()
// always equal -1
Is there a way to read the CRC without extracting the nested zip to the FS? 有没有一种方法可以读取CRC而不将嵌套的zip提取到FS? Why doesn't it work as is? 为什么它不能按原样工作?
zStream = new ZipInputStream(new ZipFile(zipPath).getInputStream(zEntry)) zStream =新的ZipInputStream(新的ZipFile(zipPath).getInputStream(zEntry))
That won't work. 那行不通。 Try this: 尝试这个:
ZipInputStream innerZip = new ZipInputStream(zStream);
after you've got to the (outer) ZEntry
concerned, then iterate innerZip
's own ZEntries and get their CRCs etc, whatever you want. 在涉及到(外部) ZEntry
之后,无论您想要什么,都可以迭代innerZip
自己的ZEntries并获取其CRC等。
If reading one entry after another, the implementation of java.util.zip.ZipInputStream does not deliver the CRC of the current entry. 如果一个接一个地读取一个条目,则java.util.zip.ZipInputStream的实现不会传递当前条目的CRC。 It is available (for the previous entry) only after calling getNextEntry() again! 仅在再次调用getNextEntry()后,它才可用(对于上一个条目)!
AFAIK this is not documented but urgently needs to. AFAIK没有记录,但迫切需要。
This really appears to be a bug/limitation of the ZipEntry interface. 这确实似乎是ZipEntry界面的错误/局限。 There should be an API to calculate the CRC. 应该有一个用于计算CRC的API。
Here is some Groovy code to generate a missing CRC: 这是一些Groovy代码来生成丢失的CRC:
long calculateCRC( zStream) {
def crc32 = new CRC32()
crc32.update( IOUtils.toByteArray(zStream))
return crc32.value
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.