Reading and Writing Compressed Files
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 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 ...
Get Java Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.