Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zip files without any metadata

I'd like to find an easy way to zip a bunch of files without any file metadata (e.g., timestamps). The zip command seems to always perserve the metadata. I don't see a way to disable metadata.

I'd like the solution be a command or at most a python script. Thanks.

like image 276
user1424739 Avatar asked Apr 09 '26 12:04

user1424739


1 Answers

As some of the posts have already pointed out most of the metadata fields in a zip header have to be present. If the file contents you are zipping are identical each time, then the only field that will be different is the timestamp.

It is possible to force the timestamp to be specific value by creating a ZipInfo object and changing the timestamp.

Here is a proof-of-concept

import zipfile

file = zipfile.ZipFile("test.zip", "w")
name = "/tmp/testfile.txt"
zi = zipfile.ZipInfo.from_file(name)
zi.date_time = (1980,1,1,0,0,0)

with file.open(zi, mode='w') as member:
    with open(name, mode='rb') as file:
        fileContent = file.read()
        member.write(fileContent)

Above code creates test.zip with the field timestamp hard-wired to 1st Jan 1980.

$ unzip -l test.zip
Archive:  test.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
      307  1980-01-01 00:00   tmp/testfile.txt
---------                     -------
      307                     1 file
like image 181
pmqs Avatar answered Apr 11 '26 02:04

pmqs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!