In this post we'll see how to use GZIP to compress and decompress a file in Java. You will mainly use GZIP tool to compress and decompress files in Unix systems. Here note that you can only compress a single file using GZIP not multiple files residing in a folder.
Refer Creating Tar File And GZipping Multiple Files - Java Program to see how to gzip multiple files in Java.
Steps to gzip a file
In order to compress a file using GZIP in Java the steps are as follows-
- For reading the source file (file which has to be GZIPped) create a FileInputStream.
- Create a FileOutputStream to the target file (output GZIPped file).
- Create a GZIPOutputStream wrapping the FileOutputStream.
- Then you just need to read from the input stream and write to the output stream.
Steps to decompress a file
In order to decompress a file using GZIP in Java the steps are as follows -
- For reading the compressed file create a FileInputStream.
- Wrap that FileInputStream with in a GZIPInputStream.
- Create a FileOutputStream to the new file (created on decompressing GZIP file).
- Then you just need to read from the input stream and write to the output stream.
Java example for compressing and decompressing file in GZIP format
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; public class GZipDemo { public static void main(String[] args) { // Path to file which is gzipped String SOURCE_FILE = "G:\\Test\\abc.txt"; // Path to gzipped output files String GZIP_OUTPUT_FILE = "G:\\Test\\abc.gz"; // File you get after decompressings String GZIP_NEW_FILE = "G:\\Test\\newabc.txt"; GZipDemo gZipDemo = new GZipDemo(); try { // Compressing a file gZipDemo.compressGZipFile(SOURCE_FILE, GZIP_OUTPUT_FILE); // decompressing a file gZipDemo.deCompressGZipFile(GZIP_OUTPUT_FILE, GZIP_NEW_FILE); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * Method to gzip a file * @param sourceFile * @param outputFile * @throws IOException */ public void compressGZipFile(String sourceFile, String outputFile) throws IOException{ FileInputStream fis = new FileInputStream(sourceFile); FileOutputStream fos = new FileOutputStream(outputFile); GZIPOutputStream gZIPOutputStream = new GZIPOutputStream(fos); byte[] buffer = new byte[1024]; int len; while((len = fis.read(buffer)) > 0){ gZIPOutputStream.write(buffer, 0, len); } // Keep it in finally fis.close(); gZIPOutputStream.close(); } /** * Method to decompress a gzip file * @param gZippedFile * @param newFile * @throws IOException */ public void deCompressGZipFile(String gZippedFile, String newFile) throws IOException{ FileInputStream fis = new FileInputStream(gZippedFile); GZIPInputStream gZIPInputStream = new GZIPInputStream(fis); FileOutputStream fos = new FileOutputStream(newFile); byte[] buffer = new byte[1024]; int len; while((len = gZIPInputStream.read(buffer)) > 0){ fos.write(buffer, 0, len); } // Keep it in finally fos.close(); gZIPInputStream.close(); } }That's all for this topic Compress And Decompress File Using GZIP Format in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Java Programs Page
Related Topics
- How to Untar a File - Java Program
- Zipping files in Java
- Unzipping files in Java
- Reading all files in a folder - Java Program
- Writing file in Java
You may also like-
- How to create PDF from XML using Apache FOP
- Producer consumer Java Program using volatile
- How to run threads in sequence - Java Program
- Comparing Enum to String - Java Program
- How HashMap internally works in Java
- Semaphore in Java concurrency
- Serialization Proxy Pattern in Java
- Polymorphism in Java