Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

net



The net package contains additional classes which work with the Java net package.

JarClassLoader

The JarClassLoader class is a ClassLoader which can be used to load classes and resources from a search path of URLs referring to both JAR files and directories.

NestableURLConnection

The NestableURLConnection class is an URLConnection which can access URLs in nested archives (for example a resource in a jar nested in another jar). Note that:
  • This class does not allow to get an OutputStream to write in the archive
  • For an URL which does not correspond to a nested archive, the regular Java getInputStream() method will be used internally, so this class may be used even for not nested URLs

Gzip entries

Note that by default only the zip protocol is handled for internal entries, but if you set the NestableURLConnection.setAcceptAlternateProtocols(boolean) to true, gzip entries will be handled (they are entries with a ".gz" extension).

For example, suppose the following zip file structure:
      zipfile.zip
      -- directchild.txt
      -- gzipfile.gz
You 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);
   }

Usage specificities

Since several versions the JDK does now explicitly throw a MalformedURLException if you try to create an URL with a nested jar protocol, such as for example[1]
It worked in JDK8u92, but for example trying to create a nested URL with the jar protocol creates a MalformedURLException in JDK8u271
:
   URL url = new URL("jar:jar:file:/D:/samples/File.zip!/71812_file!/myFile.properties");
   // throw a java.net.MalformedURLException: Nested JAR URLs are not supported
and 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: zip
It 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);

Example

Suppose that we want to read the lines in an entry which is a zip file inside a zip file.
   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();
   }

NestableURLConstructor

The NestableURLConstructor class allows to created a nested URL for the zip protocol. Note that to use the URLs created by this class, you must use the ZipStreamHandlerFactory, because by default the JDK does not handle the zip protocol.

For example:
   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]
See ZipStreamHandlerFactory for how to handle the zip" protocol
.

It means that you might have to use one of the constructors with the 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);

ZipStreamHandlerFactory

By default the JDK does not support the zip protocol, which means that this code will throw a 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: zip
The ZipStreamHandlerFactory class allows to add the support for the zip protocol in the JVM. Just perform:
  ZipStreamHandlerFactory.installFactory();

Example

This code will not throw any exception:
   ZipStreamHandlerFactory.installFactory();
   URL url = new URL("zip:zip:file:/D:/samples/File.zip!/71812_file!/myFile.properties");

Notes

  1. ^ It worked in JDK8u92, but for example trying to create a nested URL with the jar protocol creates a MalformedURLException in JDK8u271
  2. ^ See ZipStreamHandlerFactory for how to handle the zip" protocol

Categories: net | packages

Copyright 2006-2024 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences