从 byte[] 读取时提取 ZipFile 条目的内容 (Java)
我有一个 zip 文件,其内容显示为 byte[] 但原始文件对象无法访问。我想阅读每个条目的内容。我能够从字节的 ByteArrayInputStream 创建 ZipInputStream,并且可以读取条目及其名称。但是我看不到一种简单的方法来提取每个条目的内容。
(我看过 Apache Commons,但也看不到一个简单的方法)。
更新@Rich的代码似乎解决了问题,谢谢
QUERY为什么两个示例的乘数都是* 4(128/512和1024 * 4)?
I have a zip file whose contents are presented as byte[] but the original file object is not accessible. I want to read the contents of each of the entries. I am able to create a ZipInputStream from a ByteArrayInputStream of the bytes and can read the entries and their names. However I cannot see an easy way to extract the contents of each entry.
(I have looked at Apache Commons but cannot see an easy way there either).
UPDATE @Rich's code seems to solve the problem, thanks
QUERY why do both examples have a multiplier of * 4 (128/512 and 1024*4) ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果要处理流中的嵌套 zip 条目,请参阅 这个答案提供想法。因为内部条目是按顺序列出的,所以可以通过获取每个条目的大小并从流中读取那么多字节来处理它们。
更新了一个将每个条目复制到标准输出的示例:
If you want to process nested zip entries from a stream, see this answer for ideas. Because the inner entries are listed sequentially they can be processed by getting the size of each entry and reading that many bytes from the stream.
Updated with an example that copies each entry to Standard out:
计算下一个 ZipEntry 的开始有点棘手。请参阅 JDK 6 中包含的示例,
It's a little bit tricky to calculate the start of next ZipEntry. Please see this example included in JDK 6,
它实际上使用
ZipInputStream
作为InputStream
(但不要在每个条目结束时关闭它)。It actually uses the
ZipInputStream
as theInputStream
(but don't close it at the end of each entry).