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
.
The
GNU
gzip/gunzip utilities
originated on Unix and are commonly used to compress files. Unlike
the PkZip format discussed in Section 9.19, these
programs do not combine the functionality of archiving and
compressing, and are therefore 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
or
BufferedReader
.
3.14.251.57