没有任何元数据的 zip 文件 [英] zip files without any metadata

查看:58
本文介绍了没有任何元数据的 zip 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到一种简单的方法来压缩一堆没有任何文件元数据(例如时间戳)的文件.zip 命令似乎总是保留元数据.我看不到禁用元数据的方法.

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.

我希望解决方案是一个命令或者最多是一个 python 脚本.谢谢.

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

推荐答案

由于一些帖子已经指出 zip 标头中的大多数元数据字段必须存在.如果每次压缩的文件内容都相同,则唯一不同的字段是时间戳.

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.

可以通过创建 ZipInfo 对象并更改时间戳来强制时间戳为特定值.

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

这是一个概念验证

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)

以上代码创建了 test.zip,字段时间戳硬连线到 1980 年 1 月 1 日.

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

这篇关于没有任何元数据的 zip 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆