?
Solved

unzip a zip file.

Posted on 2003-03-21
5
Medium Priority
?
450 Views
Last Modified: 2010-03-31
hello:
I need a unzip example which unzips a zip file that may/may not contain subdirectories in them. I came acrosss a few examples on the web that throw an exception if there are sub directories.
Would appreciate if there are some scripts for this.
Thanks a lot.
0
Comment
Question by:lotsofquestions
  • 3
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
wide_awake earned 375 total points
ID: 8184282
here's an example from kickjava.com:

import java.io.*;
import java.util.*;
import java.util.zip.*;


public class Unzip {

  public static final void copyInputStream(InputStream in, OutputStream out)
  throws IOException
  {
    byte[] buffer = new byte[1024];
    int len;

    while((len = in.read(buffer)) >= 0)
      out.write(buffer, 0, len);

    in.close();
    out.close();
  }

  public static final void main(String[] args) {
    Enumeration entries;
    ZipFile zipFile;

    if(args.length != 1) {
      System.err.println("Usage: Unzip zipfile");
      return;
    }

    try {
      zipFile = new ZipFile(args[0]);

      entries = zipFile.entries();

      while(entries.hasMoreElements()) {
        ZipEntry entry = (ZipEntry)entries.nextElement();

        if(entry.isDirectory()) {
          // Assume directories are stored parents first then children.
          System.err.println("Extracting directory: " + entry.getName());
          // This is not robust, just for demonstration purposes.
          (new File(entry.getName())).mkdir();
          continue;
        }

        System.err.println("Extracting file: " + entry.getName());
        copyInputStream(zipFile.getInputStream(entry),
           new BufferedOutputStream(new FileOutputStream(entry.getName())));
      }

      zipFile.close();
    } catch (IOException ioe) {
      System.err.println("Unhandled exception:");
      ioe.printStackTrace();
      return;
    }
  }
0
 

Author Comment

by:lotsofquestions
ID: 8196974
Hi: thank u. But this code is also not working for me. I get a FileNotFoundException.

cheers
0
 
LVL 3

Expert Comment

by:wide_awake
ID: 8197675
What filename are you supplying as an argument?

0
 

Author Comment

by:lotsofquestions
ID: 8207765
I gave the whole path - something like
c:/test/data.zip.the error happens when its reading a file in a subfolder in the zip - ex: /path1/file.txt.

But I kind of modified the original code for my purposes. THe problem was the file in the subfolders were getting read before their parent directories, hence the Filenotfound error.So before copying a file,(ex /path1/file.txt) i get its parent(path1) and then use mkdirs whether its in a subfolder or not. If it had read path1 before reading path1/file.txt your code would have worked. i am not sure if there is any other straightforward way. this is kind of quick and dirty but seems to work for now.
I give you the points for your effort.
Thanks a lot.
0
 
LVL 3

Expert Comment

by:wide_awake
ID: 8211546
Right - that code assumes that a directory entry will always exist before a file inside that directory.  I wasn't sure if zip files worked that way or not.

One way to do it would be to process all the ZipEntries for directories before any real files.  To make sure that you get all the root directories before their subdirs, you could order them by directory-name-length.

ie. "/a" would get extracted before "/arc" and "/bar" would get extracted before "/bar/foo".

Then once you've got the directory structure, just stick all the files where they're supposed to go.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month8 days, 19 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question