使用 Java ZipOutputStream 和 BufferedOutputStream 的首选方式 [英] Preferred way to use Java ZipOutputStream and BufferedOutputStream

查看:36
本文介绍了使用 Java ZipOutputStream 和 BufferedOutputStream 的首选方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Java 中,我是否实例化 ZipOutputStream 首先,还是 BufferedOutputStream 先?示例:

In Java does it matter whether I instantiate a ZipOutputStream first, or the BufferedOutputStream first? Example:

FileOutputStream dest = new FileOutputStream(file);
ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(dest));

// use zip output stream to write to

或者:

FileOutputStream dest = new FileOutputStream(file);
BufferedOutputStream out = new BufferedOutputStream(new ZipOutputStream(dest));

// use buffered stream to write to

在我的非科学时间安排中,我似乎无法分辨出这里有多大区别.我在 Java API 中看不到任何内容表明这些方法之一是否必要或首选.有什么建议吗?似乎先压缩输出然后缓冲它以进行写入会更有效.

In my non-scientific timings I can't seem to tell much of a difference here. I can't see anything in the Java API that says if one of these ways is necessary or preferred. Any advice? It seems like compressing the output first and then buffering it for writes would be more efficient.

推荐答案

您应该始终将 BufferedOutputStreamZipOutputStream 包装在一起,而不是相反.看下面的代码:

You should always wrap the BufferedOutputStream with the ZipOutputStream, never the other way around. See the below code:

FileOutputStream fos = new FileOutputStream("hello-world.zip");
BufferedOutputStream bos = new BufferedOutputStream(fos);
ZipOutputStream zos = new ZipOutputStream(bos);

try {
    for (int i = 0; i < 10; i++) {
        // not available on BufferedOutputStream
        zos.putNextEntry(new ZipEntry("hello-world." + i + ".txt"));
        zos.write("Hello World!".getBytes());
        // not available on BufferedOutputStream
        zos.closeEntry();
    }
}
finally {
    zos.close();
}

正如评论所说的 putNextEntry()closeEntry() 方法在 BufferedOutputStream 上不可用.不调用这些方法 ZipOutputStream 会抛出异常 java.util.zip.ZipException: no current ZIP entry.

As the comments say the putNextEntry() and closeEntry() methods are not available on the BufferedOutputStream. Without calling those methods ZipOutputStream throws an exception java.util.zip.ZipException: no current ZIP entry.

为了完整起见,值得注意的是,finally 子句仅在 ZipOutputStream 上调用了 close().这是因为按照惯例,所有内置的 Java 输出流包装器实现都会传播关闭.

For the sake of completeness, it is worth noting that the finally clause only calls close() on the ZipOutputStream. This is because by convention all built-in Java output stream wrapper implementations propagate closing.

我只是以相反的方式对其进行了测试.事实证明,用 BufferedOutputStream 包装一个 ZipOutputStream 然后只在它上面调用 write()(不创建/关闭条目)不会抛出一个ZipException.相反,生成的 ZIP 文件将被破坏,其中没有任何条目.

I just tested it the other way around. It turns out that wrapping a ZipOutputStream with BufferedOutputStream and then only calling write() on it (without creating / closing entries) will not throw a ZipException. Instead the resulting ZIP file will be corrupt, without any entries inside it.

这篇关于使用 Java ZipOutputStream 和 BufferedOutputStream 的首选方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆