我Path
在虚拟文件系统(jimfs)上有一个压缩文件,我需要使用ZipFile
.
但是没有构造函数ZipFile
作为Path
参数,只有File
.
但是,我无法从我Path
的 a File
(path.toFile()) 创建,因为我得到UnsupportedOperationException
. 如何打开我的 zip 文件ZipFile
?或者也许还有其他方法可以处理不在默认文件系统上的 zip 文件?
该类ZipFile
仅限于文件系统中的文件。
另一种方法是使用ZipInputStream
。InputStream
从您的Path
使用中创建一个
Path path = ...
InputStream in = Files.newInputStream(path, openOptions)
并使用InputStream
来创建ZipInputStream
. 这种方式应该可以按预期工作:
ZipInputStream zin = new ZipInputStream(in)
所以推荐的使用方法是:
try (ZipInputStream zin = new ZipInputStream(Files.newInputStream(path))) {
// do something with the ZipInputStream
}