Chapter 24. The java.util.jar PackageThe java.util.jar package contains classes for reading and writing Java archive, or JAR, files. A JAR file is nothing more than a ZIP file whose first entry is a specially named manifest file that contains attributes and digital signatures for the ZIP file entries that follow it. Many of the classes in this package are relatively simple extensions of classes from the java.util.zip package. Figure 24-1 shows the class hierarchy of this package. Figure 24-1. The java.util.jar packageThe easiest way to read a JAR file is with the random-access JarFile class. This class allows you to obtain the JarEntry that describes any named file within the JAR archive. It also allows you to obtain an enumeration of all entries in the archive and an InputStream for reading the bytes of a specific JarEntry. Each JarEntry describes a single entry in the archive and allows access to the Attributes and the digital signatures associated with the entry. The JarFile also provides access to the Manifest object for the JAR archive; this object contains Attributes for all entries in the JAR file. Attributes is a mapping of attribute name/value pairs, of course, and the inner class Attributes.Name defines constants for various standard attribute names. You can also read a JAR file with JarInputStream. This class requires to you read each entry of the file sequentially, however. JarOutputStream allows you to write out a JAR file sequentially. Finally, you can also read an entry within a JAR file and manifest attributes for that entry with a java.net.JarURLConnection object.
This class is a java.util.Map that maps the attribute names of a JAR file manifest to arbitrary string values. The JAR manifest format specifies that attribute names can contain only the ASCII characters A to Z (uppercase and lowercase), the digits 0 through 9, and the hyphen and underscore characters. Thus, this class uses Attributes.Name as the type of attribute names, in addition to the more general String class. Although you can create your own Attributes objects, you more commonly obtain Attributes objects from a Manifest.
Hierarchy: Object-->java.util.jar.Attributes(Cloneable,java.util.Map) Passed To: java.util.jar.Attributes.Attributes() Returned By: java.net.JarURLConnection.{getAttributes(), getMainAttributes()}, JarEntry.getAttributes(), Manifest.{getAttributes(), getMainAttributes()}
This class represents the name of an attribute in an Attributes object. It defines constants for the various standard attribute names used in JAR file manifests. Attribute names can contain only ASCII letters, digits, and the hyphen and underscore characters. Any other Unicode characters are illegal.
Passed To: java.util.jar.Attributes.getValue() Type Of: Too many fields to list.
This class extends java.util.zip.ZipEntry; it represents a single file in a JAR archive and the manifest attributes and digital signatures associated with that file. JarEntry objects can be read from a JAR file with JarFile or JarInputStream, and they can be written to a JAR file with JarOutputStream. Use getAttributes() to obtain the Attributes for the entry. Use getCertificates() to obtain a java.security.cert.Certificate array that contains the certificate chains for all digital signatures associated with the file.
Hierarchy: Object-->java.util.zip.ZipEntry(Cloneable,java.util.zip.ZipConstants)-->JarEntry Passed To: JarEntry.JarEntry() Returned By: java.net.JarURLConnection.getJarEntry(), JarFile.getJarEntry(), JarInputStream.getNextJarEntry()
Signals an error while reading or writing a JAR file.
Hierarchy: Object-->Throwable(Serializable)-->Exception-->java.io.IOException-->java.util.zip.ZipException-->JarException
This class represents a JAR file and allows the manifest, file list, and individual files to be read from the JAR file. It extends java.util.zip.ZipFile, and its use is similar to that of its superclass. Create a JarFile by specifying a filename or File object. If you do not want JarFile to attempt to verify any digital signatures contained in the JarFile, pass an optional boolean argument of false to the JarFile() constructor. In Java 1.3, temporary JAR files can be automatically deleted when they are closed. To take advantage of this feature, pass ZipFile.OPEN_READ|ZipFile.OPEN_DELETE as the mode argument to the JarFile() constructor. Once you have created a JarFile object, obtain the JAR Manifest with getManifest(). Obtain an enumeration of the java.util.zip.ZipEntry objects in the file with entries(). Get the JarEntry for a specified file in the JAR file with getJarEntry(). To read the contents of a specific entry in the JAR file, obtain the JarEntry or ZipEntry object that represents that entry, pass it to getInputStream(), and then read until the end of that stream. JarFile does not support the creation of new JAR files or the modification of existing files.
Hierarchy: Object-->java.util.zip.ZipFile(java.util.zip.ZipConstants)-->JarFile Returned By: java.net.JarURLConnection.getJarFile()
This class allows a JAR file to be read from an input stream. It extends java.util.ZipInputStream and is used much like that class is used. To create a JarInputStream, simply specify the InputStream from which to read. If you do not want the JarInputStream to attempt to verify any digital signatures contained in the JAR file, pass false as the second argument to the JarInputStream() constructor. The JarInputStream() constructor first reads the JAR manifest entry, if one exists. The manifest must be the first entry in the JAR file. getManifest() returns the Manifest object for the JAR file. Once you have created a JarInputStream, call getNextJarEntry() or getNextEntry() to obtain the JarEntry or java.util.zip.ZipEntry object that describes the next entry in the JAR file. Then, call a read() method (including the inherited versions) to read the contents of that entry. When the stream reaches the end of file, call getNextJarEntry() again to start reading the next entry in the file. When all entries have been read from the JAR file, getNextJarEntry() and getNextEntry() return null.
Hierarchy: Object-->java.io.InputStream-->java.io.FilterInputStream-->java.util.zip.InflaterInputStream-->java.util.zip.ZipInputStream(java.util.zip.ZipConstants)-->JarInputStream
This class can write a JAR file to an arbitrary OutputStream. JarOutputStream extends java.util.zip.ZipOutputStream and is used much like that class is used. Create a JarOutputStream by specifying the stream to write to and, optionally, the Manifest object for the JAR file. The JarOutputStream() constructor starts by writing the contents of the Manifest object into an appropriate JAR file entry. It is the programmer's responsibility to ensure that the contents of the JAR entries written subsequently match those specified in the Manifest object. This class provides no explicit support for attaching digital signatures to entries in the JAR file. After creating a JarOutputStream, call putNextEntry() to specify the JarEntry or java.util.zip.ZipEntry to be written to the stream. Then, call any of the inherited write() methods to write the contents of the entry to the stream. When that entry is finished, call putNextEntry() again to begin writing the next entry. When you have written all JAR file entries in this way, call close(). Before writing any entry, you may call the inherited setMethod() and setLevel() methods to specify how the entry should be compressed. See java.util.zip.ZipOutputStream.
Hierarchy: Object-->java.io.OutputStream-->java.io.FilterOutputStream-->java.util.zip.DeflaterOutputStream-->java.util.zip.ZipOutputStream(java.util.zip.ZipConstants)-->JarOutputStream
This class represents the manifest entry of a JAR file. getMainAttributes() returns an Attributes object that represents the manifest attributes that apply to the entire JAR file. getAttributes() returns an Attributes object that represents the manifest attributes specified for a single file in the JAR file. getEntries() returns a java.util.Map that maps the names of entries in the JAR file to the Attributes objects associated with those entries. getEntries() returns the Map object used internally by the Manifest. You can edit the contents of the Manifest by adding, deleting, or editing entries in the Map. read() reads manifest entries from an input stream, merging them into the current set of entries. write() writes the Manifest out to the specified output stream.
Hierarchy: Object-->Manifest(Cloneable) Passed To: java.net.URLClassLoader.definePackage(), JarOutputStream.JarOutputStream(), Manifest.Manifest() Returned By: java.net.JarURLConnection.getManifest(), JarFile.getManifest(), JarInputStream.getManifest() Copyright © 2001 O'Reilly & Associates. All rights reserved. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|