Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Java Tip 83: Use filters to access resources in Java archives

Roll your own zip filters and cache resources to create application-specific views on Java archives

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Starting with JDK 1.1, archive files were used to bundle together such arbitrary Java resources as class files, image files, and sound files. They are now used to shrink-wrap logically related resources that collectively define applications (for example, Web applications), libraries (Java 2 platform), beans (EJB), and applets.

The java.util.jar and java.util.zip packages let Java applications programmatically access archive files. With the diverse set of resource types that an archive file can store, there is a need for better tools that read, query, and load these resources from Java applications. The figure below illustrates the classes and interfaces that I implement in this article, and which provide a code foundation for such tools.

Classes and interfaces implemented in this article

John Mitchell, Arthur Choi, and Todd Sundsted have demonstrated how to extract data from archive files in previous JavaWorld articles (see the Resources section below for links). For the purposes of this article, I'm assuming that you're familiar with these basic concepts. I'll start by explaining what a Java archive filter is and walk through some example source code. Next, I'll implement the JarClassTable class. JarClassTable demonstrates how you can combine archive filters with a simple caching system to create simple, easy-to-define views of resources in Java archive files. You'll see how easy this all is in Java.

What is a Java archive filter?

An archive file is simply a zip file with entries of various types. You want to be able to load archive files of varying sizes and apply filters to them. The JDK provides ways in which you can access and load the complete contents of an archive, but does not supply code with which you can access specific resources within the file. In order to do that, or create application-defined views of zip files, you'll need an archive filter.

You implement archive filters in much the same way that you would pass a java.io.FileFilter to the list() method of java.io.File in order to get specific files in a directory. In the following example, SuffixZipEntryFilter, a zip filter, is used by JarInfo to provide a view of an archive file. The full source code for SuffixZipEntryFilter and JarInfo is packaged in the jar file in Resources.

ZipEntryFilter classFilter = new SuffixZipEntryFilter(".class");
JarInfo jinfo              = new JarInfo("yourarchive.jar", classFilter);
System.out.println( jinfo );


The code above displays all entries within yourarchive.jar whose zip entry names end with .class. Like java.io.FileFilter, ZipEntryFilter is an interface that has a single accept() method; however, it filters on ZipEntry, not java.io.File.

public interface ZipEntryFilter
{
  public boolean accept( ZipEntry ze );   
}


SuffixZipEntryFilter implements ZipEntryFilter, placing the filter logic inside the accept() method.

public class SuffixZipEntryFilter implements ZipEntryFilter
{    
  private String fSuffix;
 
  public SuffixZipEntryFilter(String suffix)
  {
    fSuffix = suffix;
  }
  public boolean accept( ZipEntry ze )
  {
    if( ze == null || fSuffix == null )
      return false;         
    return ze.getName().endsWith(fSuffix);
  }  
}


JarInfo uses zip entry filters to weed out unacceptable resources. The following example demonstrates how JarInfo's constructor takes both a zip file and a zip entry filter to determine which zip entries it should extract from the archive:

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources