我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
}