user2727195 user2727195 - 4 months ago 11
Bash Question

Unix zip command is updating existing archive instead of creating a new one

$(which zip) -r ../data.zip .


The contents of the folders are changing dynamically overtime, I tested it first with including
huge.pdf
file and later deleted it, but still
data.zip
stays the same size. On unzipping I could see the
huge.pdf
file there.

and I'm executing this shell command in an application (nodejs)

Answer

You can use -FS argument to filesync :

$(which zip) -FSr ../data.zip .

All files not present in current directory and already present in the zip will be deleted from it and all new files will be added to it.

From man zip :

-FS
       --filesync
              Synchronize the contents of an archive with the files on the OS.  Normally when an archive is updated, new files are added and changed files are updated but files that no longer  exist
              on  the  OS  are  not deleted from the archive.  This option enables a new mode that checks entries in the archive against the file system.  If the file time and file size of the entry
              matches that of the OS file, the entry is copied from the old archive instead of being read from the file system and compressed.  If the OS file has changed, the entry is read and com‐
              pressed  as  usual.   If the entry in the archive does not match a file on the OS, the entry is deleted.  Enabling this option should create archives that are the same as new archives,
              but since existing entries are copied instead of compressed, updating an existing archive with -FS can be much faster than creating a new archive.  Also consider using -u for  updating
              an archive.
Comments