This example show you how to apply gzip compression to a file in Java.

Java supports both Zip and GZip compression out of the box. You do not need any third party components.

import java.io.*;
import java.util.zip.*;

public class GzipCompressor
{

	public void Compress(String inputFile, String outputFile)
	{
		try
		{
			BufferedReader reader =
				new BufferedReader(new FileReader(inputFile));

			BufferedOutputStream output =
				new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(outputFile)));

			int b;
			while((b =reader.read()) != -1)
			{
				output.write(b);
			}

			reader.close();
			output.close();
		}
		catch(IOException e)
		{
			System.out.println(e.getMessage());
		}
	}
}

Tagged with:
 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>