简体   繁体   中英

zip files without any metadata

I'd like to find an easy way to zip a bunch of files without any file metadata (eg, 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM