如何通过单个 InputStream 读取多个 ZipEntries

发布于 2024-09-15 22:28:04 字数 437 浏览 11 评论 0原文

我必须将输入流作为参数传递给第三方库,该库将从输入流中读取完整的内容并完成其工作。

我的问题是,我的一些文件是 Zip 文件 - 具有多个 ZipEntry。据我了解,可以一次读取一个 zipEntry,然后执行 zipInputStream.getNextEntry(),然后再次读取,依此类推。但是,第 3 方库不理解这一点,并且需要单个输入流。 zip 文件的所有 zipEntries 都应作为单个 inputStream 提供。

请赐教我如何去做。我无法使用 ZipFile,因为该文件未存储在本地(在不同的服务器中)。我也无法读取所有 zipEntries 并构造 ByteArrayOutputStream 或字符串,因为文件可能非常大,这会增加内存使用量。

我想要一种方法让一个 inputStream 透明地从单个 zip 文件的多个 zip 条目中读取。

提前致谢, 普拉萨纳

I have to pass an InputStream as a parameter to a 3rd party library, which will read the complete contents from the InputStream and do its job.

My problem is, some of my files are Zip files - with more than one ZipEntry. From what I understand, one can read one zipEntry at a time and then do a zipInputStream.getNextEntry() and then again read and so on. But, the 3rd party library doesn't understand this and expects a single InputStream. All the zipEntries of a zip file should be available as a single inputStream.

Please enlighten me as to how to do it. I cannot use ZipFile as the file is not stored locally (in a different server). I also cannot read all zipEntries and construct a ByteArrayOutputStream or a string as the files can be very big and that will spike memory usage.

I want a way to let one inputStream read from multiple zip entries of a single zip file transparantly.

thanks in advance,
Prasanna

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朱染 2024-09-22 22:28:04

我不是专家,但我看到有两种方法可以满足您的要求:

虽然我不会称其为最有效的方法,但相当简单的方法是将它们全部写入缓冲区,然后使用该缓冲区来提供数据输入流。您可以考虑使用 ByteArrayOutputStream,向其提供 ZipEntries,然后使用相同的 buf 创建一个 ByteArrayInputStream

或者,SequenceInputStream 听起来像是您所追求的,但我没有机会详细研究它。

巴拉德

I'm no expert but I see two ways of doing what you are asking for:

While I wouldn't call it the most efficient way, the fairly foolproof method is to write them all to a buffer and then use that buffer to feed an inputstream. You could look into using a ByteArrayOutputStream, feed it your ZipEntries, and then create a ByteArrayInputStream with the same buf

Alternatively the SequenceInputStream sounds like it does what you are after, but I've not had a chance to look into it in much detail.

K.Barad

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文