How to compress and un-compress the data of a file in Java?


Java provides a two classes namely, DeflaterOutputStream and InflaterInputStream for compressing and uncompressing data.

Compressing a Single file

To compress a single file −

  • Create a FileInputStream object, by passing the path of the file to be compressed in String format, as a parameter to its constructor.
  • Create a FileOutputStream object, by passing the path of the output file, in String format, as a parameter to its constructor.
  • Create a DeflaterOutputStream object, by passing the above created FileOutputStream object, as a parameter to its constructor.
  • Then, read the contents of the input file and write using the write() method of the DeflaterOutputStream class.

Example

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.DeflaterOutputStream;
public class CompressingFiles {
   public static void main(String args[]) throws IOException {
      //Instantiating the FileInputStream
      String inputPath = "D:\ExampleDirectory\logo.jpg";
      FileInputStream inputStream = new FileInputStream(inputPath);
      //Instantiating the FileOutputStream
      String outputPath = "D:\ExampleDirectory\compressedLogo.txt";
      FileOutputStream outputStream = new FileOutputStream(outputPath);
      //Instantiating the DeflaterOutputStream
      DeflaterOutputStream compresser = new DeflaterOutputStream(outputStream);
      int contents;
      while ((contents=inputStream.read())!=-1){
         compresser.write(contents);
      }
      compresser.close();
      System.out.println("File compressed.......");
   }
}

Output

File compressed.......

Uncompressing the compressed file

To un-compress a compressed file −

  • Create a FileInputStream object, by passing the path of the compressed file in String format, as a parameter to its constructor.
  • Create a FileOutputStream object, by passing the path of the output file (uncompressed image file), in String format, as a parameter to its constructor.
  • Create a InflaterInputStream object, by passing the above created FileOutputStream object, as a parameter to its constructor.
  • Then, read the contents of the InflaterInputStream object and write using the write() method of the FileOutputStream class.

Example

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.InflaterInputStream;
public class DeCompressingFiles {
   public static void main(String args[]) throws IOException {
      StringinputPath ="D:\ExampleDirectory\compressed.txt";
      //Instantiating the FileInputStream
      FileInputStream inputStream = new FileInputStream(inputPath);
      String outputpath = "D:\ExampleDirectory\output.jpg";
      FileOutputStream outputStream = new FileOutputStream(outputpath);
      InflaterInputStream decompresser = new InflaterInputStream(inputStream);
      int contents;
      while ((contents=decompresser.read())!=-1){
         outputStream.write(contents);
      }
      //close the file
      outputStream.close();
      decompresser.close();
      System.out.println("File un-compressed.......");
   }
}

Output

File un-compressed.......

Updated on: 02-Aug-2019

708 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements