Provider not found exception when creating a FileSystem for my zip?

3.9k Views Asked by At

I have created a Zip file on a JimFS FileSystem instance. I would now like to read the Zip using the Java FileSystem API.

Here is how I create the FileSystem:

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

However, this throws an error:

java.nio.file.ProviderNotFoundException: Provider not found

Interestingly, the code works with the default FileSystem.

  • What does this error mean?
  • How should I create my Zip FileSystem?
3

There are 3 best solutions below

1
byteit101 On BEST ANSWER

This is not supported before JDK 12 via that specific constructor (Path, ClassLoader)

This was fixed in JDK12, with commit 196c20c0d14d99cc08fae64a74c802b061231a41

The offending code was in ZipFileSystemProvider in JDK 11 and earlier:

        if (path.getFileSystem() != FileSystems.getDefault()) {
            throw new UnsupportedOperationException();
        }
1
sdgfsdh On

This works, but it seems hacky and crucially I'm not sure why it works.

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<>());
        }
    }
}
0
Ilya Serbis On

Check whether source path points to the zip archive file.

In my case it pointed to the ordinary text file which even had other than '.zip' extension.