Home arrow Java & J2EE arrow Page 3 - GZIPping with Java

Enough… Let’s Write Some GZIP Code - Java

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.

  1. GZIPping with Java
  2. GZIP vs. ZIP
  3. Enough… Let’s Write Some GZIP Code
  4. And the Other Way Around: Uncompressing Your GZIP Files (Programmatically)
  5. Leveraging GZIP in the Web World
By: Kulvir Singh Bhogal and Javid Jamae
Rating: starstarstarstarstar / 30
February 18, 2004

print this article



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);

// Finish creation of gzip file

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.

>>> More Java & J2EE Articles          >>> More By Kulvir Singh Bhogal

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: