wood burning stoves 2.0*
The moose likes Java in General and the fly likes Zip Directory Recursively Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Zip Directory Recursively" Watch "Zip Directory Recursively" New topic
Author

Zip Directory Recursively

victor kamat
Ranch Hand

Joined: Jan 10, 2007
Posts: 247
I need to programmatically zip up a directory recursively.

Can you please guide me to what I should be looking.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Obviously, File.listFiles() is your best friend in this example. Without it, recursion is going to be hard.

That said, the biggest issue is building the folder structure inside your ZIP file. You can't use only getName() when creating ZipEntry objects, since then you loose the path information. Using the absolute path is also not a good idea, since it will add too much path information. toString() looks like a good idea but has the same problem as the absolute path if the file is absolute.

I've solved this in the past by specifying the parent path myself:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Michael Schnell
Greenhorn

Joined: Aug 31, 2009
Posts: 2
Hi there!

You could use Utils4J: Utils4J / Zip a directory

Regards
Michael
André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
Please what can I pass as argument to ZipOutputStream ???
André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
I know I must pass the directory where the compressed will appear... but how?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Just create a ZipOutputStream and wrap it around an OutputStream which sends the data where you want it to go.

Since apparently you want it to go to a file, then a FileOutputStream would be a good choice. So:

André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
ohhhh paul... I tried but my method failed... sorry
André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
I have created into C: a folder named teste50 that has into it a child folder named 1 the has two .doc files, so, the method can find but compress only the folder no compress her children the files... Look at below please:

André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
I think the method is not complete...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41106
    
  45
André Asantos wrote:the method can find but compress only the folder no compress her children the files...

Of course the files are missing - the else block that is supposed to handle files is empty.


Ping & DNS - my free Android networking tools app
André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
Could you help me please?

I am trying but facing difficult, look at:

Throws IOException:

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41106
    
  45
If you really want people to help you, you need to get in the habit to TellTheDetails. What do you think an exception name is worth without either the exception message or the line of code in which it happens?
André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
I got it...

So, I would like to get a complete method that works that, a method that compressess folders/directories beucase I was just trying... only an attempt...
André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
Could you help me with this method it crashed when reading a folder:

André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
sorry that works but not 100% right... let me explain better:

exemple:
if you try to compress a folder named exemple folder1 that has a child named file1 together another file named file2 that compresses but when you descompress the result is -> file1 and file2 instead of folder1/file1 and file2.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Zip Directory Recursively
 
Similar Threads
Class12.zip
Recursively iterate folders with Ant
Zip of a directory
Recursive Read of Files from a Directory.
Forced Directory Deletion