问题
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.
回答1:
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
来源:https://stackoverflow.com/questions/56590942/zip-files-without-any-metadata