URLConnection
which can access URLs in nested archives (for example a resource in a jar nested in another jar). Note that:getInputStream()
method will be used internally, so this class may be used even for not nested URLszipfile.zip -- directchild.txt -- gzipfile.gzYou can read the content of the "gzipfile.gz" file by:
File zipFile = ... // my zipfile.zip file String zipfileURL = zipFile.toURI().toURL().toString(); String urlSpec = "jar:" + zipfileURL + "!/gzipfile.gz"; URL url = new URL(urlSpec); NestableURLConnection conn = new NestableURLConnection(url); conn.setAcceptAlternateProtocols(true); InputStream stream = conn.getInputStream(); try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) { String line = reader.readLine(); System.out.println(line); }or:
File file = ... // my zipfile.zip file URL url = file.toURI().toURL(); NestableURLConstructor constructor = new NestableURLConstructor(url); constructor.addNestedEntry("gzipfile.gz"); String spec = constructor.getURLSpec(); URL url = new URL(urlSpec); NestableURLConnection conn = new NestableURLConnection(url); conn.setAcceptAlternateProtocols(true); InputStream stream = conn.getInputStream(); try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) { String line = reader.readLine(); System.out.println(line); }
MalformedURLException
if you try to create an URL with a nested jar protocol, such as for example[1]
MalformedURLException
in JDK8u271URL url = new URL("jar:jar:file:/D:/samples/File.zip!/71812_file!/myFile.properties"); // throw a java.net.MalformedURLException: Nested JAR URLs are not supportedand additionally the JDK does not support the zip protocol. The following code will also throw a
MalformedURLException
:URL url = new URL("zip:zip:file:/D:/samples/File.zip!/71812_file!/myFile.properties"); // throw a java.net.MalformedURLException: unknown protocol: zipIt means that to use this class you will need to use the ZipStreamHandlerFactory class. For example:
ZipStreamHandlerFactory.installFactory(); URL url = new URL("zip:zip:file:/D:/samples/File.zip!/71812_file!/myFile.properties"); NestableURLConnection conn = new NestableURLConnection(url);
ZipStreamHandlerFactory.installFactory(); String zipfileURL = zipfile.toURL().toString(); String urlSpec = "zip:" + zipfileURL + "!/" + link; URL url = new URL(urlSpec); NestableURLConnection conn = new NestableURLConnection(url); InputStream stream = conn.getInputStream(); ZipInputStream zipStream = new ZipInputStream(stream); ZipEntry entry = null; String entryName = null; while (true) { entry = zipStream.getNextEntry(); }
File file = new File("D://samples/File.zip"); URL url = file.toURI().toURL(); NestableURLConstructor constructor = new NestableURLConstructor(url); constructor.addNestedEntry("71812_file"); constructor.addNestedEntry("myFile.properties"); String spec = constructor.getURLSpec(); // spec is "zip:zip:file:/D:/samples/File.zip!/71812_file!/myFile.properties"Note that by default the wrapper protocol will be "zip:", which is not handled by default by the JDK[2]
useZipProtocol
argument set to false. For example:File file = new File("D://samples/File.zip"); URL url = file.toURI().toURL(); NestableURLConstructor constructor = new NestableURLConstructor(url, false); constructor.addNestedEntry("71812_file"); constructor.addNestedEntry("myFile.properties"); String spec = constructor.getURLSpec(); URL url = new URL(spec);
MalformedURLException
by default:URL url = new URL("zip:zip:file:/D:/samples/File.zip!/71812_file!/myFile.properties"); // throw a java.net.MalformedURLException: unknown protocol: zipThe ZipStreamHandlerFactory class allows to add the support for the zip protocol in the JVM. Just perform:
ZipStreamHandlerFactory.installFactory();
ZipStreamHandlerFactory.installFactory(); URL url = new URL("zip:zip:file:/D:/samples/File.zip!/71812_file!/myFile.properties");
MalformedURLException
in JDK8u271Copyright 2006-2024 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences