Open In App

Compressing and Decompressing files in Java

Improve
Improve
Like Article
Like
Save
Share
Report

DeflaterOutputStream and InflaterInputStream  classes are provided in Java to compress and decompress the file contents. These classes provide useful methods that can be used for compressing  the file content.

Compressing a File using DeflaterOutputStream

This class implements an output stream filter for compressing data in the “deflate” compression format. It is also used as the basis for other types of compression filters, such as GZIPOutputStream.

Important Methods:

  • void close() : Writes remaining compressed data to the output stream and closes the underlying stream.
  • protected void deflate() : Writes next block of compressed data to the output stream.
  • void finish() : Finishes writing compressed data to the output stream without closing the underlying stream.
  • void flush() : Flushes the compressed output stream.
  • void write(byte[] b, int off, int len) : Writes an array of bytes to the compressed output stream where off is the start offset of data and len is total number of bytes.
  • void write(int b) : Writes a byte to the compressed output stream.

Steps to compress a file(file 1)

  • Take an input file ‘file 1’ to FileInputStream for reading data.
  • Take the output file ‘file 2’ and assign it to FileOutputStream . This will help to write data into ‘file2’.
  • Assign FileOutputStream to DeflaterOutputStream for Compressing the data.
  • Now, read data from FileInputStream and write it into DeflaterOutputStream. It will compress the data and send it to FileOutputStream which stores the compressed data into the output file.




import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
  
class zip
{
    public static void main(String[] args) throws IOException {
        //Assign the original file : file to
        //FileInputStream for reading data
        FileInputStream fis=new FileInputStream("file1");
  
        //Assign compressed file:file2 to FileOutputStream
        FileOutputStream fos=new FileOutputStream("file2");
  
        //Assign FileOutputStream to DeflaterOutputStream
        DeflaterOutputStream dos=new DeflaterOutputStream(fos);
  
        //read data from FileInputStream and write it into DeflaterOutputStream
        int data;
        while ((data=fis.read())!=-1)
        {
            dos.write(data);
        }
  
        //close the file
        fis.close();
        dos.close();
    }
}


Decompressing a File using InflaterInputStream

This class implements a stream filter for uncompressing data in the “deflate” compression format. It is also used as the basis for other decompression filters, such as GZIPInputStream.

Important methods:

  • int available() : Returns 0 after EOF has been reached, otherwise always return 1.
  • void close() : Closes the input stream and releases any system resources associated with the stream.
  • protected void fill() : Fills input buffer with more data to decompress.
  • void mark(int readlimit) : Marks the current position in the input stream.
  • boolean markSupported() : Tests if the input stream supports the mark and reset methods.
  • int read() : Reads a byte of uncompressed data.
  • int read(byte[] b, int off, int len) : Reads decompressed data into an array of bytes to the compressed output stream where off is the start offset of data and len is total number of bytes.
  • void reset() : Re-positions this stream to the position at the time the mark method was last called on this input stream.

Steps to decompress a file

  • File with the name ‘file2’ now contains compressed data and we need to obtain original decompressed data from this file.
  • Assign the compressed file ‘file2’ to FileInputStream. This helps to read data from ‘file2’.
  • Assign the output file ‘file3’ to FileOutputStream. This will help to write uncompressed data into ‘file3’.
  • Now, read uncompressed data from InflaterInputStream and write it into FileOutputStream. This will write the uncompressed data to ‘file3’.




import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.InflaterInputStream;
  
//Uncompressing a file using an InflaterInputStream
class unzip
{
    public static void main(String[] args) throws IOException {
        //assign Input File : file2 to FileInputStream for reading data
        FileInputStream fis=new FileInputStream("file2");
  
        //assign output file: file3 to FileOutputStream for reading the data
        FileOutputStream fos=new FileOutputStream("file3");
          
        //assign inflaterInputStream to FileInputStream for uncompressing the data
        InflaterInputStream iis=new InflaterInputStream(fis);
          
        //read data from inflaterInputStream and write it into FileOutputStream 
        int data;
        while((data=iis.read())!=-1)
        {
            fos.write(data);
        }
          
        //close the files
        fos.close();
        iis.close();
          
    }
}


 



Last Updated : 11 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads