如何在写入时压缩文件?
有谁知道如何压缩您正在写入的流?我试图避免将文件写入磁盘,并且想知道是否可以在将数据写入流时压缩数据。
这背后的用例是原始文件将非常大,我们希望避免将解压缩的整个文件写入磁盘。
Does anyone know of a way to zip a stream that you are writing to? I'm trying to avoid writing the file to the disk, and was wondering if I can just compress data as you are writing it to the stream.
The use case behind this is that the raw file is going to be very large and we want to avoid having to write the entire thing unzipped onto the disk.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想您会对 ZipOutputStream< 感兴趣/a>.您可以写入该流,然后将其写入压缩文件。
另外,请查看本教程,了解在 Java 中使用压缩文件 。以下是该教程的一个片段,可能是一个有用的说明:
I think you would be interested in ZipOutputStream. You can write to that stream, and then write it out to a zipped file.
Also, check out this tutorial for working with compressed (zipped) files in Java. Here is a snippet from that tutorial, which might be a helpful illustration:
将另一个
OutputStream
包装在ZipOutputStream
(或GZIPOutputStream
),并调用ZipOutputStream
上的写入方法。Wrap another
OutputStream
in aZipOutputStream
(orGZIPOutputStream
), and call the write methods on theZipOutputStream
.我以前在网络应用程序中做过这个。我获得了对servlet OutputStream 的引用,然后将其提供给
ZipOutputStream
。然后我就拉了拉链。压缩文件被提供给客户端浏览器。简单的。I have done this before in a webapp. I got a reference to the servlet OutputStream and then supplied that to the
ZipOutputStream
. Then I just zipped. The zipped file was served up to the client browser. Easy.这通常是使用过滤器的 Web 应用程序完成的:
http:// /tim.oreilly.com/pub/a/onjava/2003/11/19/filters.html
This is commonly done for web applications using filters:
http://tim.oreilly.com/pub/a/onjava/2003/11/19/filters.html