Zipping files is appending by default

By admin
This is not a bug in the age-old zip Linux program. It’s maybe a bug in its intuitiveness.

I have a piece of automation that downloads a zip file from a file storage cache (GitHub Actions actions/cache in this case). Then, it unpacks it, and plucks some of the files from it into another fresh new directory. Lastly, it creates a new .zip file with the same name. The same name because that way, when the process is done, it uploads the new .zip file into the file storage cache. But be careful; does it really create a new .zip file?

To demonstrate the surprise:

$ cd /tmp/

$ mkdir somefiles
MONEY

$ touch somefiles/file1.txt $ touch somefiles/file2.txt $ zip -r somefiles.zip somefiles adding: somefiles/ (stored

0%
PERCENT

) adding: somefiles/file1.txt (stored

0%
PERCENT

) adding:

somefiles/file2.txt
ORG

(stored

0%
PERCENT

)

Now we have a somefiles.zip to work with. It has

2
CARDINAL

files in it.

Next session. Let’s say it’s

another day
DATE

and a fresh new /tmp directory and the previous somefiles.txt has been downloaded from the

first
ORDINAL

session. This time we want to create a new somefile directory but in it, only have file2.txt from before and a new file file3.txt .

$ rm -fr somefiles $ unzip somefiles.zip Archive: somefiles.zip creating: somefiles/ extracting: somefiles/file1.txt extracting:

somefiles/file2.txt $ rm somefiles/file1.txt $
ORG

touch somefiles/file3.txt $ zip -r somefiles.zip somefiles updating: somefiles/ (stored

0%
PERCENT

) updating:

somefiles/file2.txt
ORG

(stored

0%
PERCENT

) adding: somefiles/file3.txt (stored

0%
PERCENT

)

And here comes the surprise, let’s peek into the newly zipped up somefiles.txt (which was made from the somefiles/ directory which only contained file2.txt and file3.txt ):

$ rm -fr somefiles $ unzip -l somefiles.zip Archive: somefiles.zip Length Date Time Name ——— ———- —– —- 0

2023-10-04 16:06
DATE

somefiles/ 0

2023-10-04 16:05
DATE

somefiles/file1.txt 0

2023-10
DATE

-04 16:06

somefiles/file2.txt
ORG

0

2023-10
DATE

-04 16:06 somefiles/file3.txt ——— ——-

0 4
CARDINAL

files

I did not see that coming! The command zip -r somefiles.zip somefiles/ doesn’t create a fresh new .zip file based on recursively walking the somefiles directory. It does an append by default!