Unzip a file into proper directory structure

J

Java and Swing

I have a directory structure that I am going to zip up, such as...

c:\
foo\
abc.txt
bar\
myinfo.doc
mynotes.xls
zoo\
animals\
elephant.jpg

so I zip up the entire C: directory (in this case it only has three
directories and four files). So when I attempt to unzip this file on
some other hard drive, I want it to extract the files and put them into
their correct directories, i.e. foo\, bar\, zoo\animals. How do i do
this? I know I can extract a file from the zip, but don't I have to
first make sure the path exists before I can extract it properly?

thanks
 
J

Java and Swing

I got it. Basically you get the zip entries and has you loop through
them you can call, ZipEntry.isDirectory() ....if it is, then create a
new File object, and then File.mkdirs().
 
D

Daniel

From my experience it is not exactly that easy. You will get away with
that most of the time.
From what I understand is that the path can be stored differently in
the zipfile depending on who creates the zip.
First there is the one version you vae encountered, where you have
directory nodes. This is easy to handle, as you discovered.
second there is the case where the name of the file include the full
path, but no directory nodes are written, so you have to use the name
of the file to know where it goes.

last is a little gotcha for the solution you have.. you do not always
know if the directory nodes will appear before the files in the file.
so that means that you may have to go through the zip twice, once for
finding all directories, and one for extracting files..

much more of this is available online, basicaly use google to find
mroe posts regarding this.

regards
Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top