Is it possible to replace a file in a zip file without unzipping deleting the old file adding the new file and rezipping it back?
Reason is I have a zip file which i
I know this is old question, but I wanted to do the same. Update a file in zip archive. And none of the above answers really helped me.
Here is what I did. Created temp directory abc
. Copied file.zip
to abc
and extracted the file in that directory. I edited the file I wanted to edit.
Then while being in abc
, ran the following command
user@host ~/temp/abc $ zip -u file.zip
updating: content/js/ (stored 0%)
updating: content/js/moduleConfig.js (deflated 69%)
-u
switch will look for changed/new files and will add to the zip archive.
yes its possible.
on linux based systems just install zip and you can call it in the command line. have a look at the manpage: http://linux.die.net/man/1/zip
but in my personal experience, if possible and compression is not so important, this works better with plain tar files and tar.
I've found the Linux zip
file to be cumbersome for replacing a single file in a zip. The jar
utility from the Java Development Kit may be easier. Consider the common task of updating WEB/web.xml
in a JAR file (which is just a zip file):
jar -uf path/to/myapp.jar -C path/to/dir WEB-INF/web.xml
Here, path/to/dir
is the path to a directory containing the WEB-INF
directory (which in turn contains web.xml
).
You can use: zip -u file.zip path/file_to_update
From zip(1):
When given the name of an existing zip archive, zip will replace identically named entries in the zip archive or add entries for new names.
So just use the zip
command as you normally would to create a new .zip file containing only that one file, except the .zip filename you specify will be the existing archive.
From the side of ZIP archive structure - you can update zip file without recompressing it, you will just need to skip all files which are prior of the file you need to replace, then put your updated file, and then the rest of the files. And finally you'll need to put the updated centeral directory structure. However, I doubt that most common tools would allow you to make this.