gpt4 book ai didi

python - ZipFile 获取创建日期

转载 作者:行者123 更新时间:2023-12-01 00:58:15 30 4
gpt4 key购买 nike

我想获取压缩文件夹内文件的创建日期。

我知道如果没有 zip,这可以通过使用 os.path.getctime() 来实现可以使用 ZipInfo.date_time 提取压缩文件夹内文件的函数和上次修改日期。但是ZipInfo似乎没有办法提取创建日期。

另外,我尝试过使用ZipInfo获取修改日期如下。

# zip_file is the .zip folder
# screenshot_filename is the file inside .zip
with ZipFile(zip_file, 'r') as my_zip:
my_zip.getinfo(screenshot_filename)

还有ZipInfo对象结果不包含任何 date_time信息。下面是示例。

<ZipInfo filename='SCREEN CAP/SS.png' compress_type=deflate external_attr=0x20 file_size=555790 compress_size=504859>

那么,我是否做错了,或者是否有更好的方法来提取压缩文件夹内文件的创建日期(或修改日期,如果创建日期不可能)?

更新:

我得到了获取最后修改时间的答案/date_time来自ZipInfo 。显然,虽然date_time没有在对象中列出,我们可以通过访问属性来获取它,即

my_zip.getinfo(screenshot_filename).date_time

但是,我仍在寻找获取创建日期的答案。

最佳答案

默认情况下,ZIP 文件仅存储文件的修改日期(本地时间 with 2 seconds precision ,继承自 FAT file system limitations )。任何其他元数据都可以存储在 extra 字段中。

Note: Python does NOT decode extra field data, so you have to parse it yourself according to the documentation below!

extra 字段由多个紧随其后的数据 block 组成。以下额外 block 可用于以 UTC 格式存储文件创建或修改日期,并且更精确:

  • NTFS (0x000a);
  • UNIX (0x000d);
  • Info-ZIP Macintosh (0x334d "M3");
  • Unix 扩展时间戳(0x5455“UT”);
  • Info-ZIP UNIX(0x5855“UX”)。

(有关详细信息,请参阅 Info-ZIP's extra field description)

Note: As of Python 3.7 zipfile module reads file information only from ZIP's central directory file header, so you may have problems getting dates from some third-party extra blocks.
However, you could get extra field data from local file header yourself using header_offset field.

参见this answer在解压时设置创建日期。

关于python - ZipFile 获取创建日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56050989/

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