1

How can I add/modify/delete/merge recursive directory in a zip file (in Java) without file system?

Do I have to respect the order of zip entries?

Yes, I know merging directories is very complex job..

minmaxavg
  • 686
  • 6
  • 21
  • Not sure on exactly how to do it, but you might want to read through the docs here: http://docs.oracle.com/javase/6/docs/api/java/util/zip/ZipFile.html if you haven't already. – w4etwetewtwet Jul 03 '13 at 11:09
  • possible duplicate of [Appending files to a zip file with Java](http://stackoverflow.com/questions/2223434/appending-files-to-a-zip-file-with-java) – McDowell Jul 03 '13 at 11:11
  • @McDowell No, I mean "recursively", so It also means It could also merge directories, remove only specified files recursively. That's a complex job, through... And I don't want any heavy tools. – minmaxavg Jul 03 '13 at 11:13

2 Answers2

2

If you need to add whole directory with files to zip archive recursively only by Java core efforts, then you can use good example from Mkyong's blog. If you need to append files to existing zip-file, the you should use a link from @McDowell's comment: Appending files to a zip file with Java

Community
  • 1
  • 1
Alex Stybaev
  • 4,623
  • 3
  • 30
  • 44
  • And another one : That example completely ignores orders of entries. Is keeping file path orders unnecessary? – minmaxavg Jul 03 '13 at 11:19
  • This article https://blogs.oracle.com/CoreJavaTechTips/entry/creating_zip_and_jar_files says that the files are stored in the order, they were written. – Alex Stybaev Jul 03 '13 at 11:25
1

There is no simple answer, your going to need to write a faire bit of code. You can't use the JDK ZipFile class, as that only supports reading zip files.

Instead use Commons Compress. Have a look at the examples and the zip documentation to get going.

Basically you'll need to open an input zip file, and an output zip file. Read each entry in tern, and decide whether to write it to the output, transform and write, add a new entry, or skip it, . When you get to the end close both zip files.

When processing a zip file, it's not really recursive, as all the entries are just a linear list with a path and filename. The recursive part comes when a zip contains a zip, and that is quite easy to handle.

David Roussel
  • 5,788
  • 1
  • 30
  • 35