| 
     
  
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.zip.GZIPOutputStream; 
 
public class MainClass { 
  public static void main(String[] args) { 
 
    int bufferSize = 8192; 
    // create output stream 
    String sourceFileName = "data.txt"; 
    String zipname = sourceFileName + ".gz"; 
    GZIPOutputStream zipout; 
    try { 
      FileOutputStream out = new FileOutputStream(zipname); 
      zipout = new GZIPOutputStream(out); 
    } catch (IOException e) { 
      System.out.println("Couldn't create " + zipname + "."); 
      return; 
    } 
    byte[] buffer = new byte[bufferSize]; 
    // compress the file 
    try { 
      FileInputStream in = new FileInputStream(sourceFileName); 
      int length; 
      while ((length = in.read(buffer, 0, bufferSize)) != -1) 
        zipout.write(buffer, 0, length); 
      in.close(); 
    } catch (IOException e) { 
      System.out.println("Couldn't compress " + sourceFileName + "."); 
    } 
    try { 
      zipout.close(); 
    } catch (IOException e) { 
    } 
  } 
} 
 
            
          
   
    
    |