How do I deflate and inflate a file?
Author: Deron Eriksson
Description: This Java tutorial describes how to deflate and inflate a file.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)


The java.util.zip package has two classes, DeflaterOutputStream and InflaterInputStream, that make it easy to compress (deflate) and uncompress (inflate) a stream. So if we output a stream to a FileOutputStream via a DeflaterOutputStream, this will compress the data, resulting in a compressed file. On the other hand, if we have compressed file data that we are reading from a FileInputStream, we can pass it through an InflaterInputStream to uncompress the data.

This is demonstrated by the DeflaterInflaterTest class. This class reads a regular text file, "original.txt", and compresses/copies it to the "deflated.txt" file. It then reads "deflated.txt" and uncompresses/copies it to "inflated.txt".

DeflaterInflaterTest.java

package test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;

public class DeflaterInflaterTest {

	public static void main(String[] args) throws Exception {

		FileInputStream fis = new FileInputStream("original.txt");
		FileOutputStream fos = new FileOutputStream("deflated.txt");
		DeflaterOutputStream dos = new DeflaterOutputStream(fos);

		doCopy(fis, dos); // copy original.txt to deflated.txt and compress it

		FileInputStream fis2 = new FileInputStream("deflated.txt");
		InflaterInputStream iis = new InflaterInputStream(fis2);
		FileOutputStream fos2 = new FileOutputStream("inflated.txt");

		doCopy(iis, fos2); // copy deflated.txt to inflated.txt and uncompress it

	}

	public static void doCopy(InputStream is, OutputStream os) throws Exception {
		int oneByte;
		while ((oneByte = is.read()) != -1) {
			os.write(oneByte);
		}
		os.close();
		is.close();
	}

}

The original.txt consisted of the following text. The file was 790 bytes long.

original.txt

this file is uncompressed this file is uncompressed this file is uncompressed
this file is uncompressed this file is uncompressed this file is uncompressed
this file is uncompressed this file is uncompressed this file is uncompressed
this file is uncompressed this file is uncompressed this file is uncompressed
this file is uncompressed this file is uncompressed this file is uncompressed
this file is uncompressed this file is uncompressed this file is uncompressed
this file is uncompressed this file is uncompressed this file is uncompressed
this file is uncompressed this file is uncompressed this file is uncompressed
this file is uncompressed this file is uncompressed this file is uncompressed
this file is uncompressed this file is uncompressed this file is uncompressed

The content (compressed data) of deflated.txt is shown below. The file was 47 bytes long.

deflated.txt

deflated.txt

The inflated.txt file contents are shown below. As you can see, it is an exact copy of original.txt after the compressed data was inflated. The file was 790 bytes long.

inflated.txt

this file is uncompressed this file is uncompressed this file is uncompressed
this file is uncompressed this file is uncompressed this file is uncompressed
this file is uncompressed this file is uncompressed this file is uncompressed
this file is uncompressed this file is uncompressed this file is uncompressed
this file is uncompressed this file is uncompressed this file is uncompressed
this file is uncompressed this file is uncompressed this file is uncompressed
this file is uncompressed this file is uncompressed this file is uncompressed
this file is uncompressed this file is uncompressed this file is uncompressed
this file is uncompressed this file is uncompressed this file is uncompressed
this file is uncompressed this file is uncompressed this file is uncompressed