«Back to Home

Core Java

Topics

Compress And Uncompress File In Java

Compress and Uncompress file
 
In Java, DeflaterOutputStream and InflaterInputStream classes are used to compress and uncompress the data in the deflate compression format.
 
DeflaterOutputStream class

The DeflaterOutputStream class is used to compress the data in the deflate compression format in Java. It has a capability to the other compression filters like GZIPOutputStream.
 
Let’s see an example, given below, with DeflaterOutputStream class.
 
Code
  1. import java.io.*;  
  2. import java.util.zip.*;  
  3. public class CompressFile {  
  4.     public static void main(String args[]) {  
  5.         try {  
  6.             FileInputStream fis = new FileInputStream("Deflater.java");  
  7.             FileOutputStream fos = new FileOutputStream("Java.txt");  
  8.             DeflaterOutputStream d = new DeflaterOutputStream(fos);  
  9.             int i;  
  10.             while ((i = fis.read()) != -1) {  
  11.                 d.write((byte) i);  
  12.                 d.flush();  
  13.             }  
  14.             fis.close();  
  15.             d.close();  
  16.         } catch (Exception e) {  
  17.             System.out.println(e);  
  18.         }  
  19.         System.out.println("Rest program.....");  
  20.     }  
  21. }  
33

In the example, mentioned above, we read the data of a file and compress it into another file with the use of DeflaterOutputStream class. We can compress any file. At this time, we compress the Deflater.java file.
 
InflaterInputStream class

The InflaterInputStream class is used to uncompress or decompress the file in the deflate compression format in Java. It has a capability to the other decompression filters like GZIPInputStream class.
 
Let’s see an example, given below, with InflaterInputStream class.
 
Code
  1. import java.io.*;  
  2. import java.util.zip.*;  
  3. public class UncompressFile {  
  4.   
  5.     public static void main(String args[]) {  
  6.         try {  
  7.             FileInputStream fis = new FileInputStream("def.txt");  
  8.             InflaterInputStream ins = new InflaterInputStream(fis);  
  9.             FileOutputStream fos = new FileOutputStream("ABC.java");  
  10.             int i;  
  11.             while ((i = ins.read()) != -1) {  
  12.                 fos.write((byte) i);  
  13.                 fos.flush();  
  14.             }  
  15.             fis.close();  
  16.             fos.close();  
  17.             ins.close();  
  18.         } catch (Exception e) {  
  19.             System.out.println(e);  
  20.         }  
  21.         System.out.println("Rest program....");  
  22.     }  
  23. }  
34

In the example, mentioned above, we uncompress the compressed file Java.txt into Simple.java.
 
Summary

Thus, we learnt that DeflaterOutputStream and InflaterInputStream classes are used to compress and uncompress the data in the deflate compression format and also learnt how to create it in Java.