How to create a file system in a bank inside another bank

I am trying to use NIO FileSystem to access a bank inside another bank. Name the outer jar my-outer.jar and the inner my-inner.jar (Using Java 8 and Windows 7, but I think this is not a problem)

I am using the following code

String zipfilePath = "c:/testfs/my-outer.jar!/my-inner.jar"; Path path = Paths.get(zipfilePath); try(ZipFileSystem zipfs = (ZipFileSystem) FileSystems.newFileSystem(path, null)) { ... } 

but I get an exception below when trying to create this newFileSystem:

 Exception in thread "main" java.nio.file.FileSystemNotFoundException: C:\testfs\my-outer.jar!\my-inner.jar 

Please note that if I just use an external jar as a FileSystem, this works fine, and I can read and write files from it beautifully. It is simple when I try to break into the internal archive that problems begin.

Does the file system support JarURLConnection notation?

+6
source share
1 answer

As vbezhenear said, and as I myself experimented, the JarURLConnection notation in the form c:/testfs/my-outer.jar!/my-inner.jar does not seem to be implemented by the factory FileSystems.newFileSystem(Path path, ClassLoader loader) .

But you can still access the internal bank as follows:

 Path outerPath = Paths.get("c:/testfs/my-outer.jar"); try (FileSystem outerFS = FileSystems.newFileSystem(outerPath, null)) { Path innerPath = outerFS.getPath("/my-inner.jar"); try (FileSystem innerFS = FileSystems.newFileSystem(innerPath, null)) { ... } } 

- UPDATE -

If you are trying to use a URI instead of Path , you can create a ZipFileSystem , like this

 URI uri = URI.create("jar:file:/home/orto/stackoverflow/outer.jar!/inner.jar"); Map<String,String> env = Collections.emptyMap(); try(ZipFileSystem zipfs = (ZipFileSystem)FileSystems.newFileSystem(uri,env)) {...} 

But you will not be able to access outer.jar , not inner.jar

If you look at the source code of the JarFileSystemProvider , you will see

 @Override protected Path uriToPath(URI uri) { String scheme = uri.getScheme(); if ((scheme == null) || !scheme.equalsIgnoreCase(getScheme())) { throw new IllegalArgumentException("URI scheme is not '" + getScheme() + "'"); } try { String uristr = uri.toString(); int end = uristr.indexOf("!/"); uristr = uristr.substring(4, (end == -1) ? uristr.length() : end); uri = new URI(uristr); return Paths.get(new URI("file", uri.getHost(), uri.getPath(), null)) .toAbsolutePath(); } catch (URISyntaxException e) { throw new AssertionError(e); //never thrown } } 

The path is cut to the first "!" . Thus, there is no way to directly create a file system in the internal panel from the newFileSystem method.

+3
source

Source: https://habr.com/ru/post/985379/


All Articles