简体   繁体   English

java.util.zip.ZipFile.close()什么时候抛出IOException?

[英]when does java.util.zip.ZipFile.close() throw IOException?

Under what circumstances would java.util.zip.ZipFile.close() throw an IOException? 在什么情况下java.util.zip.ZipFile.close()会抛出IOException? Its method signature indicates that it can be thrown, but from the source code there doesn't seem to be any place where this could happen, unless it's in native code. 它的方法签名表明它可以被抛出,但是从源代码中似乎没有任何可能发生这种情况的地方,除非它是在本机代码中。 What corrective action, if any, could be taken at the point where that exception is caught? 如果有异常,可以采取哪些纠正措施?

From the API docs on ZipFile.close() : 来自ZipFile.close()API文档

Closing this ZIP file will close all of the input streams previously returned by invocations of the getInputStream method. 关闭此ZIP文件将关闭先前由getInputStream方法的调用返回的所有输入流。

And InputStream.close() throws an IOException , so ZipFile.close() has to throw it too. 并且InputStream.close()抛出一个IOException ,因此ZipFile.close()也必须抛出它。 According to the API docs for InputStream.close() , it throws an IOException "if an I/O error occurs". 根据InputStream.close()API文档,如果发生I / O错误,它会抛出IOException That's not very descriptive but it's casting a wide net. 这不是很具描述性,但它正在广泛展示。 InputStreams can represent streams coming from the filesystem, network, memory, etc. InputStreams can involve buffers that need to be flushed, sockets that need to be closed, resources that need to be freed, locks that need to be freed, etc. IOExceptions can happen for a variety of reasons. InputStreams可以表示来自文件系统,网络,内存等的流.InputStreams可能涉及需要刷新的缓冲区,需要关闭的套接字,需要释放的资源,需要释放的锁等等.IOExceptions可以发生的原因有很多。

From man close(2): 从男人关闭(2):

Not checking the return value of close() is a common but nevertheless serious programming error. 不检查close()的返回值是一个常见但严重的编程错误。 It is quite possible that errors on a previous write(2) operation are first reported at the final close(). 很可能首先在最后的close()上报告先前write(2)操作的错误。 Not checking the return value when closing the file may lead to silent loss of data. 关闭文件时不检查返回值可能会导致数据无声丢失。 This can especially be observed with NFS and with disk quota. 使用NFS和磁盘配额尤其可以观察到这种情况。

I'm not sure but I think IOException is thrown when one of the following events happen: 我不确定,但我认为当发生以下事件之一时抛出IOException:

  • The zip file was deleted by something/someone outside of the application. zip文件被应用程序外部的某些人删除。
  • When the drive that contains the zip file is unmounted/disconnected 当卸载/断开包含zip文件的驱动器时

A lot more events might be the reason but those are the only two I could think of right now. 更多的事件可能是原因,但那些是我现在能想到的唯一两个。

The documentation for ZipFile.close() says: ZipFile.close()的文档说:

Closing this ZIP file will close all of the input streams previously returned by invocations of the getInputStream method. 关闭此ZIP文件将关闭先前由getInputStream方法的调用返回的所有输入流。

Presumably the native close method is performing the close the InputStreams. 据推测,本机close方法正在执行关闭InputStreams。

The close method of InputStream has IOException as a checked exception. InputStreamclose方法将IOException作为已检查的异常。

The most likely cause is an out of space condition on the filesystem where the zip file is being written error in the underlying filesystem. 最可能的原因是 文件系统上的空间不足情况,其中zip文件被写入 底层文件系统中的错误。 Unless you can identify the cause and work around it on the fly, all you can do is report the condition to the user. 除非您能够识别原因并在运行中解决问题,否则您只需向用户报告该情况即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM