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

查看:179
本文介绍了使用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天全站免登陆