ZipFile 获取创建日期

ZipFile get creation date

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

我知道如果没有 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>

所以,我是不是做错了,或者有没有更好的方法来提取压缩文件夹中文件的创建日期(如果创建日期不可能,则为修改日期)?

更新:

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

my_zip.getinfo(screenshot_filename).date_time

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

默认情况下,ZIP 文件仅存储文件的修改日期(当地时间 with 2 seconds precision, inherited from 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字段由多个紧接在一起的数据块组成。以下额外块可用于存储文件 创建 或 UTC 更精确的修改日期:

  • NTFS (0x000a);
  • UNIX (0x000d);
  • Info-ZIP 麦金塔 (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 以在提取时设置创建日期。