gpt4 book ai didi

linux - 1980年以前的Python zipfile写文件

转载 作者:太空宇宙 更新时间:2023-11-04 12:22:02 25 4
gpt4 key购买 nike

我正在尝试使用 Python 3.5.2 创建一个 zip 文件 zipfile , 在 Linux 上。我试图添加的一些文件具有 1970 年 1 月 1 日的时间戳(没有实时时钟模块的嵌入式系统)。所以 zipfile 给出了一个异常(exception):

ValueError: ZIP does not support timestamps before 1980

然后,我的目标是实现一种解决方法,将这些文件添加到时间戳为 1980 年 1 月 1 日的 zip 文件中。但是,我发现很难找到合适的解决方法。

起初我以为我可以这样做:

def zinfo_from_file(fullname, arcname):
st = os.stat(fullname)
mtime = time.localtime(st.st_mtime)
date_time = mtime[0:6]
if date_time[0] < 1980:
date_time = (1980, 1, 1, 0, 0, 0)
zinfo = zipfile.ZipInfo(arcname, date_time)
return zinfo

...
zinfo = zinfo_from_file(fullname, arcname)
chunksize=512
with open(fullname, 'rb') as src, myzipfile.open(zinfo, 'w') as dest:
while True:
data = src.read(chunksize)
if not data:
break
dest.write(data)
...

然而,事实证明 myzipfile.open(zinfo, 'w')not supported until Python 3.6 . (我正在使用 Yocto 构建嵌入式 Linux,目前仅支持 Python 3.5.x。)

我想我可以尝试做 myzipfile.writestr(zinfo, ...),虽然看起来我必须将整个文件数据加载到内存中。

有没有更简单的方法来解决这个问题?

最佳答案

检查了文件的修改时间。如果你的文件没有修改时间,你可以写一行或者在里面输入。并再次压缩此文件或目录。

关于linux - 1980年以前的Python zipfile写文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45703747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com