简体   繁体   English

如何从嵌套存档中读取ZipEntry CRC(zip中的zip)

[英]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.

相关问题 如何从内存中的流式zip文件访问zipEntry - How to access a zipEntry from a streamed zip file in memory 如何使用 ZipOutputStream 创建压缩的 Zip 存档,以便 ZipEntry 的方法 getSize() 返回正确的大小? - How to create compressed Zip archive using ZipOutputStream so that method getSize() of ZipEntry returns correct size? 将csv数据移动到zip存档作为zipentry对象 - 构造函数ZipEntry(Path)未定义 - move csv data to zip archive as zipentry objects- constructor ZipEntry(Path) is undefined 如何使用ZipEntry读取位于另一个zip文件中的一个zip文件中的数据? - How to read data in one zip file located in another zipfile using ZipEntry? 尝试从ZIP存档创建ImageIcon对象时,出现PNGException“ crc损坏” - PNGException “crc corruption” when attempting to create ImageIcon objects from ZIP archive 如何使用Java NIO从嵌套的zip文件中读取文件 - How to read a file from a nested zip file using Java NIO 无法在Windows中使用ZipEntry从zip文件中提取文件夹 - Unable to extract folders from zip file using ZipEntry in windows 从Filechannels创建Zip存档 - Create Zip archive from Filechannels 从数据库中读取zip存档 - Reading zip archive from database 如何计算数据的CRC以使用zip条目的CRC进行验证? - How to calculate the CRC of data to verify it with the CRC of a zip entry?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM