Java tutorial
//package com.java2s; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.GZIPOutputStream; public class Main { public static void doCompressFile(String inFileName) { try { File file = new File(inFileName); FileOutputStream fos = new FileOutputStream(file + ".gz"); GZIPOutputStream gzos = new GZIPOutputStream(fos); FileInputStream fin = new FileInputStream(file); BufferedInputStream in = new BufferedInputStream(fin); byte[] buffer = new byte[1024]; int i; while ((i = in.read(buffer)) >= 0) { gzos.write(buffer, 0, i); } in.close(); gzos.close(); } catch (IOException e) { System.out.println("Exception is" + e); } } }