我已经在JimFS FileSystem实例上创建了一个Zip文件。现在我想使用Java来阅读FileSystem。
下面是我如何创建FileSystem
final FileSystem zipFs = FileSystems.newFileSystem(
source, // source is a Path tied to my JimFS FileSystem
null);但是,这会引发一个错误:
java.nio.file.ProviderNotFoundException:找不到提供者
有趣的是,该代码适用于默认的FileSystem。
FileSystem发布于 2021-02-02 22:04:55
在JDK 12之前,通过特定的构造函数(Path, ClassLoader)不支持这一点。
这是在JDK12中修复的,使用提交196c20c0d14d99cc08fae64a74c802b061231a41
违犯代码在JDK 11和更早版本的ZipFileSystemProvider中:
if (path.getFileSystem() != FileSystems.getDefault()) {
throw new UnsupportedOperationException();
}发布于 2017-06-09 17:04:30
这是可行的,但它似乎是无趣的,关键是我不知道它为什么工作。
public static FileSystem fileSystemForZip(final Path pathToZip) {
Objects.requireNotNull(pathToZip, "pathToZip is null");
try {
return FileSystems.getFileSystem(pathToZipFile.toUri());
} catch (Exception e) {
try {
return FileSystems.getFileSystem(URI.create("jar:" + pathToZipFile.toUri()));
} catch (Exception e2) {
return FileSystems.newFileSystem(
URI.create("jar:" + pathToZipFile.toUri()),
new HashMap<>());
}
}
}发布于 2021-01-15 15:13:58
检查source路径是否指向zip归档文件。
在我的例子中,它指向的是普通的文本文件,它的扩展名不是“.zip”。
https://stackoverflow.com/questions/44459152
复制相似问题