Modifying Zip Files in Place

Sometimes, you have a zip file with some files in it, and you need to update a file within the zip file. Let’s take an example:

unzip -l patch_1280_v2.zip

Archive:  patch_1152_v2.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2019-01-15 19:45   width_1152/
   162320  2019-01-15 19:27   width_1152/page611.png
   209867  2019-01-15 19:27   width_1152/page594.png
        0  2019-01-15 19:26   width_1152/.v2
---------                     -------
   372187                     4 files

So let’s suppose I want to add a database in a databases folder within this zip. To make things interesting, let’s assume we have multiple patch zip files, in which we want to add the same database folder and files for each.

zip makes this easy - we can just do:

zip -u patch_1152_v2.zip databases/file_to_add_or_update.db

# if we're adding multiple files, can also do
# this to add the README.md file and
# everything under databases/
zip -u patch_1152_v2.zip databases/* README.md

This will add this file under the databases folder within the zip. If it already exists, it will be updated. We can combine this with shell for loops to update a large number of zip files painlessly!