Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Zip Directory Recursively

 
victor kamat
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to programmatically zip up a directory recursively.

Can you please guide me to what I should be looking.
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Michael Schnell
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!

You could use Utils4J: Utils4J / Zip a directory

Regards
Michael
 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please what can I pass as argument to ZipOutputStream ???
 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know I must pass the directory where the compressed will appear... but how?
 
Paul Clapham
Sheriff
Posts: 21319
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ohhhh paul... I tried but my method failed... sorry
 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the method is not complete...
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you help me please?

I am trying but facing difficult, look at:

Throws IOException:

 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you help me with this method it crashed when reading a folder:

 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic