Most read:
Popular archives:
JavaWorld's new look is here!
We've upgraded the site with a fresh look-and-feel, improved topical navigation, better search, new features, and expanded
community platform. Learn more about the changes to JavaWorld.
| Oracle Compatibility Developer's Guide |
| The Explosion in DBMS Choice |
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.
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: