Zipping and Unzipping files in java

Posted By : Vakeel Malik | 15-Dec-2018

We know that some software like 'WinZip' provides zipping and unzipping of file data. In zipping the file contents, following two things could happen.

1. file contents are compressed and hence file size will be reduced.
2. file formate of data will be changed, making it unreadable formate

 

While zipping the file contents, zipping algorithm (logic) is used to in such a way that algorithm first find out which bit pattern is most often repeating, this repeated bit pattern replaced with 0,  the algorithm searches for next bit pattern which is most often repeated and replaced that second bit pattern with 1, algorithm search for next third most repeated bit pattern and this third repeated bit pattern replaced with 01, and the fourth with 10 and so on. then got the file is called zip file. 

to get back from the zip file to the original file, use a following reverse algorithm which substitutes the original bit pattern whenever particular bit pattern is found.

In Java, the classes to convert content to zip and unzip, we can use DeflaterOutputStream and InflaterInputStream, 
DelfalterOutputStream used to convert original file contents to zip file contents and InflaterInputStream used convert zip file contents to original file contents.

These file in belongs to java.util.zip.* package in java

 

Zipping file: - 

let us learn how to compressed file content using following stag
1. first of all create a text file with some content and save as file1.txt 
2. Create another text file for store zip content as file2.txt
3. Attached FileInputStream with file1 for read data from file1
4. Create an Object of FileOutputStream to write zip file and pass file2 as a parameter

 

Example:-

 

import java.io.*;
import java.util.zip.*;
class ZipFileDemo{
  public static void main(String arg[])throws IOException {
         FileInputStream fis=new FileInputStream("file1");
         FileOutputStream fos=new FileOutputStream("file2");
         DeflaterOutputStream dos=new DeflaterOutputStream(fos);
         int data;
         while((data=fis.read())!=-1){
              fos.write(data);
         }
         fis.close();
         dos.close() ;
   
    }
}


UnZipping file:-

Now, file2 contains compressed data. suppose we want to get the original Uncompressed file from the compressed(zip) file. use these following step

1. Create an object of FileInputStream and passed the parameter as file2 for reading compressed data from file2
2. Create an object of FileOutputStream to write uncompressed data. 
3. Create an object of InflaterInputStream convert compressed content to the original content.  

 

Example:-

 

import java.io.*;
import java.util.zip.*;
class UnZipFileDemo{
  public static void main(String arg[])throws Exception{
     FileInputStream fis=new FileInputStream("file2");
     FileOutputStream fos=new FileOutputStream("file3");
     InflaterInputStream iis=new InflaterInputStream(fis);
     int data;
     while((data=iis.read()) != -1){
           fos.write(data);              
    }
     fos.close();
     iis.close();  
   }

}

About Author

Author Image
Vakeel Malik

Vakeel is a bright Java Developer working in Oodles.

Request for Proposal

Name is required

Comment is required

Sending message..