Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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?

 

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.

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

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

Enumeration.hasMoreElement():
This method checks whether more element elements are present or not for the zip file which are enumerated.

Enumeration.nextElement():
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.*;
import java.util.zip.*;
import java.io.*;

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

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

      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 = in.read(buf)) 0) {
          out.write(buf, 0, len);
        }
        a = a + 1;
      }
      if(a > 0System.out.println("Files unzipped.");
      out.close();
      in.close();
    catch (IOException e) {
      System.out.println("Error: Operation failed!");
      System.exit(0);
    }
  }
}

Download this example.

                         

» View all related tutorials
Related Tags: c string reference io method type boolean key rmi ole this ai node oo create if boo example to contains

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.