如何有效地将未压缩的输入流转换为经过 gzip 压缩的输入流?

发布于 2024-08-19 20:59:10 字数 306 浏览 3 评论 0原文

用户将一个大文件上传到我的网站,我想对该文件进行 gzip 压缩并将其存储在 blob 中。所以我有一个未压缩的输入流,并且 blob 需要一个输入流。我知道如何使用 GZIPOutputStream 将输入流压缩为输出流,但是如何从 gzip 压缩后的输出流返回到 blob 所需的输入流。

我能找到的唯一方法是使用 ByteArrayOutputStream,然后使用 toByteArray 创建一个新的 InputStream。但这意味着我在内存中拥有该文件的完整副本。如果 JDBC 驱动程序实现也将流转换为 byte[],我不会感到惊讶,这样我就会在内存中拥有两个副本。

A user uploads a large file to my website and I want to gzip the file and store it in a blob. So I have an uncompressed InputStream and the blob wants an InputStream. I know how to compress an InputStream to an Outputstream using GZIPOutputStream, but how do I go from the gzip'ed OutputStream back to the InputStream needed by the blob.

The only way I could find involves using ByteArrayOutputStream and then creating a new InputStream using toByteArray. But that will mean I have an entire copy of the file in memory. And it wouldn't surprise me if the JDBC driver implementation converted the stream to a byte[] also so I'd have two copies in memory.

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

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

发布评论

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

评论(1

情定在深秋 2024-08-26 20:59:10

如果您使用的是 java 1.6,则可以使用 java.util.zip.DeflaterInputStream。据我所知,这正是您想要的。如果您不能使用 1.6,您应该能够使用 java.util.zip.Deflater 重新实现 DeflaterInputStream。从 BLOB 读回数据时,使用 InflaterInputStream 作为过滤器来取回原始数据。

If you are on java 1.6 you can use java.util.zip.DeflaterInputStream. As far as I can tell, this does exactly what you want. If you can't use 1.6 you should be able to reimplement DeflaterInputStream using java.util.zip.Deflater. When reading the data back from the BLOB use a InflaterInputStream as a filter to get the original data back.

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