The gzip format is the de facto standard compression format in the UNIX and Linux worlds. In a previous Dev Shed article titled Zip Meets Java, Kulvir demonstrated how to use the java.util.zip package to programmatically manipulate files in the ZIP format. In this article, we’ll cover how to use the java.util.zip package to create and read files using the gzip format.
Programmatically creating a file in the gzip format is rather straightforward. In the code below, we do just that. We start off by creating a new GZIPOutputStream object. The constructor of GZIPOutputStream takes a FileOutputStream object. We instantiate the FileOutputStream object using a String that specifies the location where our gzip file should be created.
Next, we create an InputFileStream that points to the (uncompressed) file that we wish to compress. We create a byte[] to use as a buffer to write to the GZIPOutputStream. Then we use a while loop to read our input file and write it to our GZIPOutputStream using our buffer. The process of creating the GZIP file is completed by using the finish and close methods on the GZIPOutputStream. Our sample code will create a file named examplegzip.gz in a directory named c:\articles, using the file statebirds.txt from the same directory as the source file to be zipped.
System
.out.println("Creating gzip file."); // Specify gzip file name String gzipFileName = "c:\articles\examplegzip.gz"; GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzipFileName));
// Specify the input file to be compressed String inFilename = “c:\articles\statebirds.txt"; FileInputStream in = new FileInputStream(inFilename);
// Transfer bytes from the input file // to the gzip output stream byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close();
// Finish creation of gzip file out.finish(); out.close();
After you are finished writing the file using the GZIPOutputStream, a checksum value representing the original (uncompressed) data is stored in the trailer of the GZIP file.
In the example above, we are reading from a file. Of course, because we are using an InputStream we could just as easily read from another data source, such as a socket.