Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Unzip a ZIP File

In this section, you will wll learn How to unzip (extract) a zip file? How to retrieve elements from a zip format file?


How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value

Unzip a ZIP File


How to unzip (extract) a zip file? How to retrieve elements from a zip format file? All these type of questions are solved through the following example. This program shows you how to extract files from a zip file in which many different files are stored in compressed format and make a single zip file.

There are various type methods and APIs are explained as follows which are used in the following program to retrieve all the compressed elements from the zip file.

This is the class of*; package of Java which is used to zip file entries.

Above method of the ZipFile class gets the files entries of the zip file format.

This method checks whether more element elements are present or not for the zip file which are enumerated.

This method gives you the next elements from the list of the files, which are compressed and stored in the zip file.

Here is the code of the program:

import java.util.*;

public class ZipRetrieveElements{
  public static void main(String[] args){
  ZipRetrieveElements zr = new ZipRetrieveElements();

  public ZipRetrieveElements() {
  OutputStream out = null;
  BufferedReader bf = 
BufferedReader(new InputStreamReader(;
  try {
  System.out.print("Eneter zip file name to unzip: ");
  String sourcefile = bf.readLine();
  System.out.println("Invalid file name!");
  else if(!new File(sourcefile).exists()){
  System.out.println("File not exist!");

  ZipInputStream in = 
new ZipInputStream(new FileInputStream(sourcefile));
  ZipFile zf = new ZipFile(sourcefile);
  int a = 0;
  for(Enumeration em = zf.entries(); em.hasMoreElements();){
  String targetfile = em.nextElement().toString();
  ZipEntry ze = in.getNextEntry();
  out = new FileOutputStream(targetfile);
  byte[] buf = new byte[1024];
  int len;
  while ((len = 0) {
  out.write(buf, 0, len);
  a = a + 1;
  if(a > 0System.out.println("Files unzipped.");
  catch (IOException e) {
  System.out.println("Error: Operation failed!");

Download this example.



Online Training Classes

Share on Google+Share on Google+

Unzip a ZIP File

Posted on: April 16, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Unzip a ZIP File   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
January 28, 2012

heloo sir... can u guide me to execute the aboue code... i used to retrive the zipped content by using this code but this code doesnot recognizes the .ZIP conetnt even thoug i entered the correct file so please give me the process to execute the above code
Subrata Thakur
October 4, 2012

how to unzip file to a zip file.please help me.
Rahul Munjal
December 12, 2012
regarding output

the code is running fine. but the output it gives is a file type element not a file folder type.that means i cant open the output file given by this code. plz help.