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

And the Other Way Around: Uncompressing Your GZIP Files (Programmatically) - 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



Decompressing a gzip file with the java.util.zip API is pretty straightforward too.  The class below decompresses the file we created in the previous section. The class begins by creating a new GZIPInputStream stream object. We pass the GZIPInputStream constructor the filename of the gzip file we want to decompress (i.e., c:\articles\examplegzip.gz). Next, we use the same byte buffer technique to read from the GZIPInputStream object and write to a FileOutputStream object. To clean things up, we close our GZIPInputStream and our OutputStream. Our sample program will create a file named statebirdsclone.txt, which is exactly the same as its original, statebirds.txt that we compressed in the previous section.

// Open the gzip file
String inFilename = "c:\articles\examplegzip.gz";
GZIPInputStream gzipInputStream =
new GZIPInputStream(new FileInputStream(inFilename));
// Open the output file
String outFilename = "c:\articles\statebirdsclone.txt";
OutputStream out = new FileOutputStream(outFilename);

// Transfer bytes from the compressed file to the output file
byte[] buf = new byte[1024];
int len;
while ((len = gzipInputStream.read(buf)) > 0) {
out.write(buf, 0, len);

// Close the file and stream

After reading the entire file using the GZIPInputStream, the read method will compare the checksum of the decompressed data with the checksum that is stored in the trailer of the GZIP file.  If the values do not match, the read method will throw an exception.

>>> 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: