In our usual life, we may need to access particular content from ZIP file often and often. We may not be interested in retrieving complete content from ZIP. It would be good idea to have a URL Handler, which will help us to access only the content of ZIP directly and/or seamlessly.
We have a built-in facility of accessing content from JAR file directly. Why should not we form the similar approach to access the content from ZIP ?.
Aim of this post is not only to explain the code which will help us to retrieve the particular content, we do get to know the way how URL handler is going to work.
Agenda:
Name the protocol Name the Java package Creating Java files required to enable URLHandler for ZIP protocol Notifying to JVM by setting in environment Notifying to JVM by setting as part of Main method
1. Name the Protocol
We have to name the protocol i.e we have to prefix a string which will help us to say what we are trying to do. Here, we will create our own protocol and we will name it as “zip”. Hence the URL format will looks like below
zip:http://www.javafundu.com/baz.zip!/afolder/a.txt
where
!/ - Separator
http://www.javafundu.com/baz.zip - points actual zip file
afolder/a.txt – is the content available in baz.zip2. Name the Java Package
-
Java files has to be placed in a package and the naming package should looks like below
<anypackagename>.protocol.<nameoftheprotocol>
Example: com.javafundu.protocol.zip 3. Create Java files URLStreamHandler and URLConnection
Handler, which will instruct JVM to call this Class to open the connection when URL.openConnection().
Handler.javapackage com.javafundu.protocol; import java.io.IOException; import java.net.URL; import java.net.URLConnection; import java.net.URLStreamHandler; public class Handler extends URLStreamHandler { @Override protected URLConnection openConnection(URL url) throws IOException { // create new instance of URLConnection return new ZIPURLConnection(url); } }
URLConnection does the actual job of unzipping and returning the subelement. URL argument in openConnection method has the URL http://… , when actual url is “zip:http://…”
ZIPURLConnection.java:package com.javafundu.protocol; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class ZIPURLConnection extends URLConnection { protected ZIPURLConnection(URL url)throws MalformedURLException { super(url); // TODO Auto-generated constructor stub } @Override public void connect() throws IOException { // TODO Auto-generated method stub } /* (non-Javadoc) * @see java.net.URLConnection#getInputStream() */ public InputStream getInputStream() throws IOException { InputStream result= null; URL url = this.url; String spec = url.getFile(); int separator = spec.indexOf('!'); /* * REMIND: we don't handle nested JAR URLs */ if (separator == -1) { throw new MalformedURLException("no ! found in url spec:" + spec); } long startTime = System.currentTimeMillis(); URL zipFileURL = new URL(spec.substring(0, separator++)); String entryName =spec.substring(separator+1); ZipInputStream zis = new ZipInputStream(zipFileURL.openStream()); ZipEntry entry = null; // check inputStream is ZIP or not if ((entry = zis.getNextEntry()) != null) { do { // System.out.println(entry.getName()); if(entryName.equals( entry.getName())) { result= zis; break; } } while ((entry = zis.getNextEntry()) != null); } else { System.out.println("Given path is not referring ZIP file"); } System.out.println( "Looking for " +entryName + " " +(System.currentTimeMillis() - startTime) + "ms"); return result; } }
Now Java implementation is ready for compilation. Bundle your application with these two class files. Assume bundled as jf.jar.
4.Notifying to JVM by setting in environment
Set the environment variable, which specifies the information of Handlers available location.
Java -Djava.protocol.handler.pkgs=”com.javafundu.protocol”
Now, your JVM will understand “zip:” as a URL authority and start works. If you create a URL with zip: , then Java will call com.javafundu.protocol.Handler to parse this URL.
URL url=new URL(“zip:http://www.javafundu.com/baz.zip!/afolder/a.txt ”)5.Notifying to JVM by setting as part of Main method
Sometime, this is very difficult to set this value in environment variable due to some restriction. Hence, we have alternate approach, which will help us to set and make use of this facility.
System.setProperty("java.protocol.handler.pkgs","com.javafundu.protocol");
We have to set this property at the earliest possible to before creating URL with zip:
If we wants to specify more than one protocol then we have to delimit using symbol. For exampleSystem.setProperty("java.protocol.handler.pkgs","com.javafundu.msaccess.protocolcom.javafundu.protocol");
Resource:
URL (Java 2 Platform SE v1.4.2) |
JarURLConnection (Java 2 Platform SE v1.4.2) |
RFC 2373: IP Version 6 Addressing Architecture
No comments:
Post a Comment