Java applications adding files to a zip file

Possible duplicate:
Adding files to a zip file using Java

I have a zip that contains several folders in it, but the important one is dir, and inside there is another folder called folder and folder contains many files that I need to update.

I now have a dir outside of the zip called dir, and in this folder with the files I need to update, so the paths are the same. How can I update these files in zip?

The tricky part is that dir is in the zip root and contains many folders, not just a folder, but I only need to update the files in the folder, I can not mess with any files from the folders but still in the directory.

Can this be done? I know that this can be done in bash using the -u modifier, but I would prefer to do it using java if possible.

Thank you for your help in this issue.

Just to be clearer

Internal ZIP / Dir / Folders / filestoupdate

Outside zip / Dir / folders / filestomoveintozip

+4
java zip archive
Feb 15 '12 at 19:41
source share
2 answers

Ok, here is the final method - this is the same method that was run before I actually got from the stackoverflow theme in the @Qwe link posted earlier, but I added a path variable so that it can add files to folders inside zip

Ok, now, how to use it in my example above, I wanted to add a file to a folder that was inside another folder, I would do it using my setting in a question like this

private void addFilesToZip(File source, File[] files, String path){ try{ File tmpZip = File.createTempFile(source.getName(), null); tmpZip.delete(); if(!source.renameTo(tmpZip)){ throw new Exception("Could not make temp file (" + source.getName() + ")"); } byte[] buffer = new byte[4096]; ZipInputStream zin = new ZipInputStream(new FileInputStream(tmpZip)); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(source)); for(int i = 0; i < files.length; i++){ InputStream in = new FileInputStream(files[i]); out.putNextEntry(new ZipEntry(path + files[i].getName())); for(int read = in.read(buffer); read > -1; read = in.read(buffer)){ out.write(buffer, 0, read); } out.closeEntry(); in.close(); } for(ZipEntry ze = zin.getNextEntry(); ze != null; ze = zin.getNextEntry()){ if(!zipEntryMatch(ze.getName(), files, path)){ out.putNextEntry(ze); for(int read = zin.read(buffer); read > -1; read = zin.read(buffer)){ out.write(buffer, 0, read); } out.closeEntry(); } } out.close(); tmpZip.delete(); }catch(Exception e){ e.printStackTrace(); } } private boolean zipEntryMatch(String zeName, File[] files, String path){ for(int i = 0; i < files.length; i++){ if((path + files[i].getName()).equals(zeName)){ return true; } } return false; } 

Thanks for the link, which ultimately is able to slightly improve this method, so that it can add files that were not in the root directory, and now I am a happy tourist :) I hope this helps someone else too

EDIT I worked a bit on this method so that it can not only add to zip, but also update files in zip

Use a method like this

 File[] files = {new File("/path/to/file/to/update/in")}; addFilesToZip(new File("/path/to/zip"), files, "folder/dir/"); 

You would not run the path (last variable) with / as it is not, as indicated in the zip entries

+8
Feb 16 '12 at 3:21
source share

Unfortunately, Java cannot update Zip files. Request for promotion, which was submitted 14 years ago ; -)

You will need to unzip it into a temporary folder, add files there and pack them again.

+2
Feb 15 '12 at 19:45
source share



All Articles