'ZipFile get creation date
I would like to get the creation date of a file inside zipped folder.
I know that without zip, this can be achieved with using os.path.getctime()
function and last modified date of a file inside zipped folder can be extracted with ZipInfo.date_time
. But ZipInfo
does not seem to have a way to extract the creation date.
Also, I have tried on using ZipInfo
to get the modified date as follows.
# 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)
And the ZipInfo
object result does not contain any date_time
information. Below is the example.
<ZipInfo filename='SCREEN CAP/SS.png' compress_type=deflate external_attr=0x20 file_size=555790 compress_size=504859>
So, did I do it wrongly or is there any better way to extract the creation date (or modified date if creation date is not possible) of a file inside zipped folder?
UPDATE:
I got the answer to get the last modified time/date_time
from ZipInfo
. Apparently, although date_time
is not listed in the object, we can get it simply by accessing the attribute, i.e.
my_zip.getinfo(screenshot_filename).date_time
However, I am still looking for the answer for getting the creation date.
Solution 1:[1]
By default ZIP file stores only the modification date of a file (local time with 2 seconds precision, inherited from FAT file system limitations). Any additional metadata can be stored in extra
field.
Note: Python does NOT decode
extra
field data, so you have to parse it yourself according to the documentation below!
The extra
field consists of multiple data blocks which immediately follow each other. The following extra blocks can be used to store file creation or modification date in UTC and with more precision:
- NTFS (0x000a);
- UNIX (0x000d);
- Info-ZIP Macintosh (0x334d "M3");
- Unix Extended Timestamp (0x5455 "UT");
- Info-ZIP UNIX (0x5855 "UX").
(see Info-ZIP's extra field description for more info)
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 getextra
field data from local file header yourself usingheader_offset
field.
See this answer to set the creation date when you extract it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |