Nested jar: URI

Viewed 93

The format of the jar: URI scheme is simply defined to be jar:<url>!/[<entry>].

Is it possible to "nest" two such URIs together and refer to a file inside an archive inside an archive this way? Something like:

jar:jar:http://example.com/!/archive!/file

This should designate /file inside an archive jar:http://example.com/!/archive, that is /archive found in a file provided by http://example.com/. However, URL.openConnection throws an exception:

java.net.MalformedURLException: no !/ in spec

I also cannot replace ! with %21 since that makes the jar: URI invalid, and I am not aware of any escaping that could be performed. Is this somehow possible? Or, as a more generic question, is it possible to store a URI with !/ inside <url> so that it is preserved?

2 Answers

As it is stated in the source code java.net.JarURLConnection does not handle nested Jar URIs.

Your example jar:jar:http://example.com/!/archive!/fileis parsed as entry archive!/file in the archive jar:http://example.com/. The latter is not a valid jar URI. You can not either escape the !/ sequences, because the text after jar: and before !/ must be a valid URI without any translation.

You can of course provide your own URLConnection implementation that supports nested Jar URIs.

While the other answer is indeed correct in the case of JarURLConnection, the use of jar: and similar URIs is not restricted to it. Such is the case of the Apache Commons VFS:

jar:// arch-file-uri[! absolute-path]

Where arch-file-uri refers to a file of any supported type, including other zip files. Note: if you would like to use the ! as normal character it must be escaped using %21.

"Normal character" means usage of the character in path to the file inside the archive, not in the inner URI. This makes jar:jar:http://example.com/!/archive!/file valid, as it is the last occurence of ! that should be used as a delimiter.

Note that the inner URI should probably still remain unescaped, thus it is impossible to use # there.

Related