3

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?
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
  • Does `source` have the format described in [the documentation](http://docs.oracle.com/javase/8/docs/technotes/guides/io/fsp/zipfilesystemprovider.html)? – VGR Jun 09 '17 at 13:51
  • @VGR `source` is just a `Path` (e.g. `/Library/Caches/example.zip`) – sdgfsdh Jun 09 '17 at 13:53
  • Does `FileSystems.newFileSystem(new URI("jar", source.toUri().toString(), null), null)` work? – VGR Jun 09 '17 at 14:02
  • That gives a `FileSystemAlreadyExistsException` – sdgfsdh Jun 09 '17 at 14:05
  • How about `FileSystems.getFileSystem(new URI("jar", source.toUri().toString(), null))`? – VGR Jun 09 '17 at 14:09

3 Answers3

1

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();
        }
byteit101
  • 3,910
  • 2
  • 20
  • 29
0

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<>());
        }
    }
}
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
0

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.

Ilya Serbis
  • 21,149
  • 6
  • 87
  • 74