?
Solved

unzip a zip file.

Posted on 2003-03-21
5
Medium Priority
?
447 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses

752 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