2

Path在虚拟文件系统(jimfs)上有一个压缩文件,我需要使用ZipFile.

但是没有构造函数ZipFile作为Path参数,只有File.

但是,我无法从我Path的 a File(path.toFile()) 创建,因为我得到UnsupportedOperationException. 如何打开我的 zip 文件ZipFile?或者也许还有其他方法可以处理不在默认文件系统上的 zip 文件?

4

1 回答 1

2

该类ZipFile仅限于文件系统中的文件。

另一种方法是使用ZipInputStreamInputStream从您的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
}
于 2016-07-24T16:24:57.480 回答