• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Zip Directory Recursively

 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there!

You could use Utils4J: Utils4J / Zip a directory

Regards
Michael
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know I must pass the directory where the compressed will appear... but how?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the method is not complete...
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you help me please?

I am trying but facing difficult, look at:

Throws IOException:

 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic