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

发布于 2024-08-31 06:09:02 字数 243 浏览 7 评论 0原文

在什么情况下会 java.util.zip.ZipFile.close() 抛出 IOException?它的方法签名表明它可以被抛出,但从源代码来看,似乎没有任何地方可能发生这种情况,除非它是在本机代码中。在捕获异常时可以采取什么纠正措施(如果有)?

Under what circumstances would java.util.zip.ZipFile.close() throw an 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

满地尘埃落定 2024-09-07 06:09:02

来自 API 文档 ZipFile.close()

关闭此 ZIP 文件将关闭之前调用 getInputStream 方法返回的所有输入流。

InputStream.close() 抛出 IOException,因此 ZipFile.close() 也必须抛出它。根据 API 文档 InputStream.close(),它会抛出一个 IOException“如果发生 I/O 错误”。这不是很具有描述性,但它正在撒一张广泛的网。 InputStreams 可以表示来自文件系统、网络、内存等的流。InputStreams 可以涉及需要刷新的缓冲区、需要关闭的套接字、需要释放的资源、需要释放的锁等。IOExceptions 可以发生的原因有多种。

From the API docs on ZipFile.close():

Closing this ZIP file will close all of the input streams previously returned by invocations of the getInputStream method.

And InputStream.close() throws an IOException, so ZipFile.close() has to throw it too. According to the API docs for InputStream.close(), it throws an IOException "if an I/O error occurs". 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.

孤凫 2024-09-07 06:09:02

从人关闭(2):

不检查 close() 的返回值是一个常见但严重的编程错误。很可能在最后的 close() 中首先报告先前 write(2) 操作的错误。关闭文件时不检查返回值可能会导致数据无提示丢失。使用 NFS 和磁盘配额尤其可以观察到这一点。

From man close(2):

Not checking the return value of close() is a common but nevertheless serious programming error. It is quite possible that errors on a previous write(2) operation are first reported at the final close(). 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.

爱冒险 2024-09-07 06:09:02

我不确定,但我认为当发生以下事件之一时会引发 IOException:

  • zip 文件被应用程序外部的某物/某人删除。
  • 当包含 zip 文件的驱动器被卸载/断开连接时,

可能有更多的事件是原因,但这是我现在能想到的唯一两个。

I'm not sure but I think IOException is thrown when one of the following events happen:

  • The zip file was deleted by something/someone outside of the application.
  • When the drive that contains the zip file is unmounted/disconnected

A lot more events might be the reason but those are the only two I could think of right now.

猛虎独行 2024-09-07 06:09:02

ZipFile.close() 的文档说:

关闭此 ZIP 文件将关闭先前通过调用 getInputStream 方法返回的所有输入流。

据推测,本机 close 方法正在执行关闭 InputStreams 的操作。

InputStreamclose 方法将 IOException 作为已检查异常。

最可能的原因是底层文件系统中写入 zip 文件的文件系统空间不足错误。除非您能够找出原因并立即解决它,否则您所能做的就是向用户报告情况。

The documentation for ZipFile.close() says:

Closing this ZIP file will close all of the input streams previously returned by invocations of the getInputStream method.

Presumably the native close method is performing the close the InputStreams.

The close method of InputStream has IOException as a checked exception.

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. Unless you can identify the cause and work around it on the fly, all you can do is report the condition to the user.

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