Compressing and Decompressing Files in Java using GzipInputStream and GzipOutputStream

File compression is a common practice to reduce file sizes for more efficient storage and transmission. In Java, the GzipInputStream and GzipOutputStream classes from the java.util.zip package provide a convenient way to compress and decompress files using the GZIP compression format. 

In this blog post, we will learn how to compress and decompress files using these classes. 

Compressing Files using GzipOutputStream

The GzipOutputStream class allows you to compress data and write it to an output stream. To compress a file using GzipOutputStream, follow these steps: 

  1. Create an instance of FileInputStream to read the source file. 
  2. Create an instance of GZIPOutputStream with a FileOutputStream as its parameter. 
  3. Read the data from the source file and write it to the GZIPOutputStream
  4. Close the streams to ensure proper resource cleanup. 

Here's an example code snippet to compress a file:

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

public class FileCompressionExample {
    public static void main(String[] args) {
        String sourceFile = "source.txt";
        String compressedFile = "compressed.gz";
        
        try (FileInputStream fis = new FileInputStream(sourceFile);
             FileOutputStream fos = new FileOutputStream(compressedFile);
             GZIPOutputStream gzipOS = new GZIPOutputStream(fos)) {
            
            byte[] buffer = new byte[1024];
            int bytesRead;
            
            while ((bytesRead = fis.read(buffer)) != -1) {
                gzipOS.write(buffer, 0, bytesRead);
            }
            
            System.out.println("File compressed successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Decompressing Files using GzipInputStream

The GzipInputStream class allows you to decompress data from an input stream. To decompress a file using GzipInputStream, follow these steps: 

  1. Create an instance of GzipInputStream with a FileInputStream as its parameter. 
  2. Create an instance of FileOutputStream to write the decompressed data to a new file. 
  3. Read the data from the GzipInputStream and write it to the FileOutputStream
  4. Close the streams to ensure proper resource cleanup. 

Here's an example code snippet to decompress a file:

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

public class FileDecompressionExample {
    public static void main(String[] args) {
        String compressedFile = "compressed.gz";
        String decompressedFile = "decompressed.txt";
        
        try (FileInputStream fis = new FileInputStream(compressedFile);
             GZIPInputStream gzipIS = new GZIPInputStream(fis);
             FileOutputStream fos = new FileOutputStream(decompressedFile)) {
            
            byte[] buffer = new byte[1024];
            int bytesRead;
            
            while ((bytesRead = gzipIS.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead);
            }
            
            System.out.println("File decompressed successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Conclusion

Using the GzipInputStream and GzipOutputStream classes, you can easily compress and decompress files in the GZIP compression format. These classes provide a convenient way to reduce file sizes for more efficient storage and transmission. By understanding how to use these classes, you can add file compression and decompression capabilities to your Java applications, enhancing their efficiency and performance.

Comments