How to overwrite existing zip file instead of updating it in Info-Zip?

33

5

To obtain a fresh zip file just like as tar does, do I have to perform rm foo.zip before executing zip?

$ mkdir foo; touch foo/bar
$ zip -r foo.zip foo
  adding: foo/ (stored 0%)
  adding: foo/bar (stored 0%)
$ rm foo/bar; touch foo/baz
$ zip -r foo.zip foo
  adding: foo/ (stored 0%)
  adding: foo/baz (stored 0%)
$ unzip -l foo.zip
Archive:  foo.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2011-10-27 07:49   foo/
        0  2011-10-27 07:49   foo/bar
        0  2011-10-27 07:49   foo/baz
---------                     -------
        0                     3 files

asari

Posted 2011-10-26T23:09:39.210

Reputation: 375

Answers

35

Use the -FS option to "file sync"

zip -FSr foo.zip foo

This will add any new files in the folder to the zip, and delete any files from the zip that aren't in the folder.

Paul

Posted 2011-10-26T23:09:39.210

Reputation: 52 173

Which version of zip has this option? My zip command does not recognise "-S" – Jayan – 2014-06-26T11:08:26.457

2The switch is -FS not -F and -S together. This has been there since zip 3.0 at least. Check man zip. – Paul – 2014-06-26T13:53:06.757

How is "new file" defined? Its filesize changes, date modified, both? Just curious – Moseleyi – 2018-05-31T04:20:31.293

This just work for Zip 3.0 and don't work for Zip 2.32 (didn't remove old files in zip pack) – Nabi K.A.Z. – 2019-07-20T23:05:13.637

You mean -FS -r, right? – avalanche1 – 2019-09-05T17:31:15.987

2

An alternative to using the -FS option (or deleting the old ZIP file), is to overwrite the existing ZIP file by having zip write to stdout and redirecting it to the designated file:

zip -r - foo >foo.zip

-r       : Add directory and its contents
-        : Instead of writing ZIP to a file, write to stdout
foo      : The directory to be zipped
>foo.zip : Redirect stdout to file foo.zip

If foo.zip exists, it will be overwritten by shell redirection, meaning you'll get a fresh new ZIP file 100% of the time, every time.

mxxk

Posted 2011-10-26T23:09:39.210

Reputation: 181

You mean output to stdout – flarn2006 – 2019-08-26T19:53:59.943