GZIP File in Java – Compression And Decompression

In this post we’ll see how to compress and decompress a file in Java using GZIP format. Using GZIP you can only compress a single file not multiple files in a directory.

See the procedure of creating a tar archive to gzip multiple files in this post- GZIP Multiple Files in Java Creating Tar Archive

Java GZIP example- Compress and Decompress file

GZIP compression– For compressing a file GZIPOutputStream is used. You need to read a file using InputStream and write it to the GZIPOutputStream to get a GZIP format file.

GZIP decompression– For decompressing a file GZIPInputStream is used. You need to read a file from GZIPInputStream and write it to the output stream to get a decompressed file.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GzipFile {
  public static void main(String[] args) {
    String SOURCE_FILE = "/home/knpcode/Documents/knpcode/postend";
    String GZIPPED_FILE = "/home/knpcode/Documents/knpcode/postend.gz";
    String DECOMPRESSED_FILE = "/home/knpcode/Documents/knpcode/postend_decomp.txt";
    GzipFile gzip = new GzipFile();
    // call to GZIP compress 
    gzip.compressGZipFile(SOURCE_FILE, GZIPPED_FILE);
    // call to GZIP decompress 
    gzip.deCompressGZipFile(GZIPPED_FILE, DECOMPRESSED_FILE);
  }
		
  public void compressGZipFile(String sourceFile, String gzipFile) {
    FileInputStream fis = null;
    FileOutputStream fos = null;
    GZIPOutputStream gZIPOutputStream = null;	
    try {
      fis = new FileInputStream(sourceFile);
      fos = new FileOutputStream(gzipFile);
      gZIPOutputStream = new GZIPOutputStream(fos); 
      byte[] buffer = new byte[1024];
      int len;
      while((len = fis.read(buffer)) > 0){
        gZIPOutputStream.write(buffer, 0, len);
      }
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }finally {
      try {
        if(fis != null) {				
          fis.close();					
        }
        if(gZIPOutputStream != null) {				
          gZIPOutputStream.close();
        }
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }
	
  public void deCompressGZipFile(String gzipFile, String destFile) {
    FileInputStream fis = null;
    FileOutputStream fos = null;
    GZIPInputStream gZIPInputStream = null;
    try {
      fis = new FileInputStream(gzipFile);
      gZIPInputStream = new GZIPInputStream(fis);
      fos = new FileOutputStream(destFile);
      byte[] buffer = new byte[1024];
      int len;
      while((len = gZIPInputStream.read(buffer)) > 0){
        fos.write(buffer, 0, len);
      }
    }catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }finally {
      try {
        if(gZIPInputStream != null) {				
          gZIPInputStream.close();
        }
        if(fos != null) {				
          fos.close();					
        }
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }
}

Related Posts

That’s all for the topic GZIP File in Java – Compression And Decompression. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.