UnZIp Files using java.util.zip Package

Java provides the java.util.zip package to compress and decompress the files, to UnZip the zip file We are using ZipInputStream Class for input stream filter for reading files in the ZIP file format, ZipEntry Class to represent the Zip file entry and ZipOutputStream Class for output stream filter for writing files in the ZIP file format

Program to UnZIp Files using java.util.zip package

package com.javaartifacts;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipInputStream;

public class UnZip
{    

    public static void main( String[] args )
    {
        
        byte[] buffer = new byte[4024];
        
        try{
       	   
    
       	FileInputStream fis = new FileInputStream("d:\\file.zip");
       	ZipInputStream zis = new ZipInputStream(fis);
       	ZipEntry ze = zis.getNextEntry();
       	
       	while(ze!=null){
    
       	      String fileName = ze.getName();
              File file = new File("d:\\"+File.separator+fileName);
                  
              FileOutputStream fos = new FileOutputStream(file); 
              
               int len;
               while ((len = zis.read(buffer)) > 0) {
          		fos.write(buffer, 0, len);
               }
               
               System.out.println("File unzip succesfully at location "+file.getPath());

               fos.close();   
               ze = zis.getNextEntry();
       	}
    
           zis.closeEntry();
       	   zis.close();
        
       }catch(FileNotFoundException fnex){
       	fnex.printStackTrace();
       }
       catch(ZipException zex){
           zex.printStackTrace();
        }
       catch(IOException ex){
       	ex.printStackTrace();
       }
    	
    }
    
}

 
Reference

  1. http://www.oracle.com/technetwork/articles/java/compress-1565076.html

Leave a Comment