18. The java.util.zip Package
Contents:
The package java.util.zip is new as of Java 1.1. It contains classes that provide support for general-purpose data compression and decompression using the ZLIB compression algorithms. The important classes in java.util.zip are those that provide the means to read and write data that is compatible with the popular GZIP and ZIP formats: GZIPInputStream, GZIPOutputStream, ZipInputStream, and ZipOutputStream. Figure 18.1 shows the class hierarchy for the java.util.zip package. It is easy to use the GZIP and ZIP classes because they subclass java.io.FilterInputStream and java.io.FilterOutputStream. For example, to decompress GZIP data, you simply create a GZIPInputStream around the input stream that represents the compressed data. As with any InputStream, you could be reading from a file, a socket, or some other data source. You can then read decompressed data by calling the read() methods of the GZIPInputStream. The following code fragment creates a GZIPInputStream that reads data from the file sample.gz :
FileInputStream inFile; try { inFile = new FileInputStream("sample.gz"); } catch (IOException e) { System.out.println("Couldn't open file."); return; } GZIPInputStream in = new GZIPInputStream(inFile); // Now use in.read() to get decompressed data. Similarly, you can compress data using the GZIP format by creating a GZIPOutputStream around an output stream and using the write() methods of GZIPOutputStream. The following code fragment creates a GZIPOutputStream that writes data to the file sample.gz :
FileOutputStream outFile; try { outFile = new FileOutputStream("sample.gz"); } catch (IOException e) { System.out.println("Couldn't open file."); return; } GZIPOutputStream out = new GZIPOutputStream(outFile); // Now use out.write() to write compressed data. A ZIP file, or archive, is not quite as easy to use because it may contain more than one compressed file. A ZipEntry object represents each compressed file in the archive. When you are reading from a ZipInputStream, you must first call getNextEntry() to access an entry, and then you can read decompressed data from the stream, just like with a GZIPInputStream. When you are writing data to a ZipOutputStream, use putNextEntry() before you start writing each entry in the archive. The ZipFile class is provided as a convenience for reading an archive; it allows nonsequential access to the entries in a ZIP file. The remainder of the classes in java.util.zip exist to support the GZIP and ZIP classes. The generic Deflater and Inflater classes implement the ZLIB algorithms; they are used by DeflaterOutputStream and InflaterInputStream to decompress and compress data. The Checksum interface and the classes that implement it, Adler32 and CRC32, define algorithms that generate checksums from stream data. These checksums are used by the CheckedInputStream and CheckedOutputStream classes. Adler32NameAdler32Synopsis
DescriptionThe Adler32 class implements the Checksum interface using the Adler-32 algorithm. This algorithm is significantly faster than CRC-32 and almost as reliable. Class Summary
public class java.util.zip.Adler32 extends java.lang.Object implements java.util.zip.Checksum { // Constructors public Adler32(); // Instance Methods public long getValue(); public void reset(); public void update(int b); public void update(byte[] b); public native void update(byte[] b, int off, int len); } ConstructorsAdler32public Adler32()
Instance MethodsgetValuepublic long getValue()
resetpublic void reset()
updatepublic void update(int b)
public void update(byte[] b)
public native void update(byte[] b, int off, int len)
Inherited Methods
See AlsoChecksum, CRC32 |
|