Accessing zips and jars using Java Part 1
In this part, we'll cover how to get the table of contents of a zip file and a jar file and how to get the manifest of a jar file. In part two (next week), we'll look at how to create, add files to, and extract files from zip files and jar files.
ToolsHere are the tools you will need to write Java programs that access zip and jar files:
- The Java Development Kit (JDK) version 1.1 or version 1.2 (Java 2). If you want to access zip files only, or want to access jar files as zip files, then JDK 1.1 will do. If you want to access both zip files and jar files, you will need Java 2.
- Your favorite text editor.
Java 2 changesJava 2 has the following changes from JDK 1.1 related to zip files and jar files:
was added. It returns the number of entries in the zip file.
was added. It is used to construct a new zip entry using the attributes of another zip entry.
was added. It is used to obtain a copy of a zip entry.
was added. It returns the hash code for the zip entry.
- Package java.util.jar was added. It contains classes for operating on jar files.
ZIP contentsGetting the table of contents of a jar file may seem difficult at first, but actually it is not. You use the ZipFile and ZipEntry classes, which are contained in package java.util.zip, and the Enumeration interface, which is contained in package java.util. Here is an example program, ZipView, that gets the table of contents of a zip file and displays it. Sample output is here.
The algorithm for ZipView is very straightforward:
- Open the zip file by creating a ZipFile, passing the zip file name to the constructor.
- Get an Enumeration of the zip entries.
- Loop through the entries. For each entry, cast it to a ZipEntry, then print the name of the entry.
- Close the zip file.
Note that I enclosed the main loop in a
block with a finally clause. This makes sure that the zip file is closed, even if an exception is thrown.
If you lookup the ZipFile class in the JDK documentation, you will see that
returns Enumeration. "The Enumeration interface provides a simple and consistent way to loop through all of the elements contained within some kind of object or data structure." 
To determine if there are more elements available, you call
. To get the next available element, you call
, which returns Object, which you have to cast to the class that you need. Also, you can only loop through an Enumeration once; there is no way to go back to the beginning. That is why in ZipView, I declared "entries" in the initialization part of the
loop. When the loop is done, "entries" is not useful, so I might as well let the garbage collector have it.
JAR contentsJar files are zip files with a manifest, which contains meta-information about the jar and the files in the jar. So, getting the table of contents of a jar file works like getting the table of contents of a zip file; but instead of the ZipFile and ZipEntry classes, you use the JarFile and JarEntry classes, which are contained in package java.util.jar. (You actually can use the zip classes, but you won't have access to the manifest.) Here is an example program, JarView, that gets the table of contents of a jar file and displays it. Sample output is here.
JAR manifestGetting the manifest of a jar file (without extracting it as a file) is slightly more difficult than getting the table of contents. This is because you have to get the manifest attributes and the manifest entries, which themselves have attributes, and because you have to you use the new collection interfaces, which are somewhat more complex than the Enumeration interface in JDK 1.1. In addition to the JarFile class, you use the Manifest and Attributes classes, which are contained in package java.util.jar, and you use the Map, Map.Entry, Set, and Iterator interfaces, which are contained in package java.util. Here is an example program, JarManifest, that gets the manifest of a jar file and displays it. Sample output is here.
The algorithm for JarManifest is fairly straightforward:
- Open the jar file.
- Get the manifest by calling
- Get an Iterator for the manifest attributes.
- Loop through the manifest attributes. For each attribute, cast it to Map.Entry, then print the key and the value (e.g., Foo: 1).
- Get an Iterator for the manifest entries.
- Loop through the manifest entries. For each entry, cast it to Map.Entry, print the key, then:
- Get an Iterator for the entry attributes.
- Loop through the manifest entry attributes. For each attribute, cast it to Map.Entry, then print the key and value.
- Close the jar file.
Iterator entries =
And, the code to get an Iterator for the attributes of a manifest entry looks like this:
Iterator attributes =
(Note the cast to Attributes. This is necessary because
Additionally, Map has an inner interface, Map.Entry. This interface is used to operate on the key-value pairs that are contained in a Map. Thus, when you get an element from the Iterator for a Set that was returned by
, you have to cast the element to Map.Entry before you can get the key and the value.
References Java in a Nutshell, Second Edition. Flanagan, David. Copyright 1997, 1996. O'Reilly & Associates, Inc.
About the authorThornton Rose is a software developer who lives and works in Atlanta, Georgia. He can be reached via e-mail at firstname.lastname@example.org.