如何在写入时压缩文件?

发布于 2024-10-04 21:06:13 字数 103 浏览 6 评论 0原文

有谁知道如何压缩您正在写入的流?我试图避免将文件写入磁盘,并且想知道是否可以在将数据写入流时压缩数据。

这背后的用例是原始文件将非常大,我们希望避免将解压缩的整个文件写入磁盘。

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 技术交流群。

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

发布评论

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

评论(4

天涯沦落人 2024-10-11 21:06:13

我想您会对 ZipOutputStream< 感兴趣/a>.您可以写入该流,然后将其写入压缩文件。

另外,请查看本教程,了解在 Java 中使用压缩文件 。以下是该教程的一个片段,可能是一个有用的说明:

     BufferedInputStream origin = null;
     FileOutputStream dest = new 
       FileOutputStream("c:\\zip\\myfigs.zip");
     ZipOutputStream out = new ZipOutputStream(new 
       BufferedOutputStream(dest));
     //out.setMethod(ZipOutputStream.DEFLATED);
     byte data[] = new byte[BUFFER];
     // get a list of files from current directory
     File f = new File(".");
     String files[] = f.list();

     for (int i=0; i<files.length; i++) {
        System.out.println("Adding: "+files[i]);
        FileInputStream fi = new 
          FileInputStream(files[i]);
        origin = new 
          BufferedInputStream(fi, BUFFER);
        ZipEntry entry = new ZipEntry(files[i]);
        out.putNextEntry(entry);
        int count;
        while((count = origin.read(data, 0, 
          BUFFER)) != -1) {
           out.write(data, 0, count);
        }
        origin.close();
     }
     out.close();

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:

     BufferedInputStream origin = null;
     FileOutputStream dest = new 
       FileOutputStream("c:\\zip\\myfigs.zip");
     ZipOutputStream out = new ZipOutputStream(new 
       BufferedOutputStream(dest));
     //out.setMethod(ZipOutputStream.DEFLATED);
     byte data[] = new byte[BUFFER];
     // get a list of files from current directory
     File f = new File(".");
     String files[] = f.list();

     for (int i=0; i<files.length; i++) {
        System.out.println("Adding: "+files[i]);
        FileInputStream fi = new 
          FileInputStream(files[i]);
        origin = new 
          BufferedInputStream(fi, BUFFER);
        ZipEntry entry = new ZipEntry(files[i]);
        out.putNextEntry(entry);
        int count;
        while((count = origin.read(data, 0, 
          BUFFER)) != -1) {
           out.write(data, 0, count);
        }
        origin.close();
     }
     out.close();
以酷 2024-10-11 21:06:13

将另一个 OutputStream 包装在 ZipOutputStream (或 GZIPOutputStream),并调用 ZipOutputStream 上的写入方法。

Wrap another OutputStream in a ZipOutputStream (or GZIPOutputStream), and call the write methods on the ZipOutputStream.

独自←快乐 2024-10-11 21:06:13

我以前在网络应用程序中做过这个。我获得了对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.

打小就很酷 2024-10-11 21:06:13

这通常是使用过滤器的 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

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