java 关闭 ZipOutputStream
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4681459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Closing ZipOutputStream
提问by Lachezar Balev
I'm a bit confused. I know that an empty zip is not legal. But what about this sample snippet:
我有点困惑。我知道空的 zip 是不合法的。但是这个示例片段怎么样:
ZipOutputStream zos = null;
try
{
zos = new ZipOutputStream(new FileOutputStream("..."));
//
//..
//
}
finally
{
zos.close();
}
If no zip entries had been added for some reason (possibly exceptional situation) then the following exception will be thrown on close attempt:
如果由于某种原因(可能是异常情况)没有添加 zip 条目,则关闭尝试时将抛出以下异常:
Exception in thread "main" java.util.zip.ZipException: ZIP file must have at least one entry
at java.util.zip.ZipOutputStream.finish(ZipOutputStream.java:304)
at java.util.zip.DeflaterOutputStream.close(DeflaterOutputStream.java:146)
at java.util.zip.ZipOutputStream.close(ZipOutputStream.java:321)
In this situation what would be the cleanest way to close the stream?
在这种情况下,关闭流的最干净方法是什么?
Thanks...
谢谢...
采纳答案by Anon
You should close the FileOutputStream
, not the ZipOutputStream
, because the former is what actually consumes system resources.
您应该关闭FileOutputStream
,而不是ZipOutputStream
,因为前者实际消耗系统资源。
File zipFile = new File("/tmp/example.zip");
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos);
// ...
zos.close();
}
catch (IOException ex)
{
// log/report exception, then delete the invalid file
IOUtils.closeQuietly(fos);
zipFile.delete();
}
finally
{
IOUtils.closeQuietly(fos);
}
The IOUtils
class is found in Jakarta Commons IO. Using it means that you don't have to deal with the possible-but-rarely-useful IOException
that can be thrown by close()
.
在IOUtils
类中发现的Jakarta Commons IO。使用它意味着你不必应付可能的,但是,很少-有用的IOException
,可以通过抛出close()
。
回答by vincentlcy
Instead of closing the stream only when things are added, what I did is a condition check to see if there was anything to zip, before running the zip code. This helped me to simplify the process and I think can be used in general to handle the "ZIP file must have at least one entry" problem. True that closing zos
may throw other exceptions, but that is rare.
我所做的不是仅在添加内容时关闭流,而是在运行邮政编码之前进行条件检查以查看是否有任何内容需要压缩。这帮助我简化了过程,我认为一般可以用来处理“ZIP 文件必须至少有一个条目”的问题。确实,关闭zos
可能会引发其他异常,但这种情况很少见。
I think it is problem with Java, that doesn't handle the case when there are no files to zip.
我认为这是 Java 的问题,当没有要压缩的文件时,它无法处理这种情况。
i.e:
IE:
int itemsToAdd=0;
//....
if ( itemsToAdd > 0 ) {
ZipOutputStream zos = new ZipOutputStream(file);
try {
//add files to zip
}
finally {
zos.close();
}
}
回答by Mihai Toader
You should track if you added stuff the zip stream and close it only when things were added:
您应该跟踪是否在 zip 流中添加了内容,并仅在添加内容时关闭它:
ZipOutputStream zos = null;
OutputStream file = new FileOutputStream("...")
int itemsAdded=0;
try
{
zos = new ZipOutputStream(file);
//
//..
// itemsAdded++;
}
finally
{
if ( itemsAdded > 0 ) {
zos.close();
} else {
file.close();
}
}
of if you don't need the count just use a boolean
flag.
如果您不需要计数,请使用boolean
标志。