Java Cookbook, Second Edition

Problem

You need to read or write files that have been compressed using GNU zip, or gzip. These files are usually saved with the extension .gz.

Solution

Use a GZipInputStream or GZipOutputStream as appropriate.

Discussion

The GNU gzip/gunzip utilities originated on Unix and are commonly used to compress files. Unlike the Zip format discussed in Recipe 10.20, these programs do not combine the functionality of archiving and compressing, and, therefore, they are easier to work with. However, because they are not archives, people often use them in conjunction with an archiver. On Unix, tar and cpio are common, with tar and gzip being the de facto standard combination. Many web sites and FTP sites make files available with the extension .tar.gz; such files originally had to be first decompressed with gunzip and then extracted with tar. As this became a common operation, modern versions of tar have been extended to support a -z option, which means to gunzip before extracting, or to gzip before writing, as appropriate.

You may find archived files in gzip format on any platform. If you do, they're quite easy to read, again using classes from the java.util.zip package. This program assumes that the gzipped file originally contained text (Unicode characters). If not, you would treat it as a stream of bytes, that is, use a BufferedInputStream instead of a BufferedReader :

import java.io.*; import java.util.zip.*; public class ReadGZIP { public static void main(String argv[]) throws IOException { String FILENAME = "file.txt.gz"; // Since there are 4 constructors here, I wrote them all out in full. // In real life you would probably nest these constructor calls. FileInputStream fin = new FileInputStream(FILENAME); GZIPInputStream gzis = new GZIPInputStream(fin); InputStreamReader xover = new InputStreamReader(gzis); BufferedReader is = new BufferedReader(xover); String line; // Now read lines of text: the BufferedReader puts them in lines, // the InputStreamReader does Unicode conversion, and the // GZipInputStream "gunzip"s the data from the FileInputStream. while ((line = is.readLine( )) != null) System.out.println("Read: " + line); } }

If you need to write files in this format, everything works as you'd expect: you create a GZipOutputStream and write on it, usually using it through a DataOutputStream, BufferedWriter, or PrintWriter.

See Also

The Inflater and Deflater classes provide access to general-purpose compression and decompression. The InflaterStream and DeflaterStream stream classes provide an I/O-based implementation of Inflater and Deflater.

Категории