3

我在 JimFSFileSystem实例上创建了一个 Zip 文件。我现在想使用 Java FileSystemAPI 阅读 Zip。

这是我创建的方法FileSystem

final FileSystem zipFs = FileSystems.newFileSystem(
    source, // source is a Path tied to my JimFS FileSystem
    null);

但是,这会引发错误:

java.nio.file.ProviderNotFoundException:未找到提供程序

有趣的是,该代码使用默认的FileSystem.

  • 这个错误是什么意思?
  • 我应该如何创建我的 Zip FileSystem
4

3 回答 3

1

Path, ClassLoader在 JDK 12 之前,通过特定的构造函数 ( )不支持此功能

这已在 JDK12 中修复,提交196c20c0d14d99cc08fae64a74c802b061231a41

有问题的代码位于 JDK 11 及更早版本的 ZipFileSystemProvider 中:

        if (path.getFileSystem() != FileSystems.getDefault()) {
            throw new UnsupportedOperationException();
        }
于 2021-02-02T22:04:55.247 回答
0

检查source路径是否指向 zip 存档文件。

在我的例子中,它指向了一个普通的文本文件,它的扩展名甚至不是“.zip”。

于 2021-01-15T15:13:58.130 回答
0

这行得通,但它似乎很老套,而且至关重要的是,我不确定它为什么会起作用。

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<>());
        }
    }
}
于 2017-06-09T17:04:30.353 回答