The java.util.jar Package
The java.util.jar package, shown in Figure 11-1, contains two stream classes and another half dozen assorted classes and interfaces that assist in reading from and writing to JAR archives. As you can see, almost everything in this package is a subclass of a related class in the java.util.zip package. JAR files are zip files, and they are read and written just like zip files. This package mostly adds support for reading and writing manifests. You don't have to use the java.util.jar package at alljava.util.zip and the standard I/O and string classes are enough to do anything you need to dobut java.util.jar certainly does make your job easier when you need to read manifest entries.
Figure 11-1. The java.util.jar package hierarchy
All of the classes in java.util.jar are used much like their superclasses are. For instance, to read a JAR file, follow these steps:
- Construct a JarInputStream object from an underlying stream, most commonly a file input stream.
- Open the next JAR entry in the archive.
- Read data from the JAR entry using InputStream methods such as read( ).
- Close the JAR entry (optional).
- Repeat steps 2 through 4 as long as there are more entries (files) remaining in the archive.
- Close the JAR input stream.
These are the same six steps you use to read a zip file, only with the java.util.zip classes replaced by their counterparts in java.util.jar.