Extract Files from Zip File and Retain Mod Date

Extract files from zip file and retain mod date?

Well, it does take a little post-processing, but it's not that bad:

import os
import zipfile
import time

outDirectory = 'C:\\TEMP\\'
inFile = 'test.zip'
fh = open(os.path.join(outDirectory,inFile),'rb')
z = zipfile.ZipFile(fh)

for f in z.infolist():
name, date_time = f.filename, f.date_time
name = os.path.join(outDirectory, name)
with open(name, 'wb') as outFile:
outFile.write(z.open(f).read())
date_time = time.mktime(date_time + (0, 0, -1))
os.utime(name, (date_time, date_time))

Okay, maybe it is that bad.

Unzipping multiple file without loosing the original creation date

Actually opening multiple zip files manually keeps the dates, at least on macOS.
Search for the files you need to unzip, select -> open.

Php keep the modified date of files extracted from the zip

I found a way to do it by using mtime value supplied by ZipArchive::statIndex

It changes the modified date of the extracted file after extraction.

Here is the final code:

$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
$filename = $mtime = $zip->statIndex(0)['name'];
$zip->extractTo('test/');
touch('test/'.$filename, $zip->statIndex(0)['mtime']); // Change the modified date of the extracted file.
$zip->close();
}

Extract files from zip file and retain mod date?

Well, it does take a little post-processing, but it's not that bad:

import os
import zipfile
import time

outDirectory = 'C:\\TEMP\\'
inFile = 'test.zip'
fh = open(os.path.join(outDirectory,inFile),'rb')
z = zipfile.ZipFile(fh)

for f in z.infolist():
name, date_time = f.filename, f.date_time
name = os.path.join(outDirectory, name)
with open(name, 'wb') as outFile:
outFile.write(z.open(f).read())
date_time = time.mktime(date_time + (0, 0, -1))
os.utime(name, (date_time, date_time))

Okay, maybe it is that bad.

ZipFile get creation date

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 get extra field data from local file header yourself using header_offset field.

See this answer to set the creation date when you extract it.

Java: Maintaining zipped files Modified Date

Use ZipEntry.getTime to get the last-modified time and File.setLastModified to set it on the file after you are done copying it.

How can I extract files from a zip folder using PHP while retaining the files original timestamps?

You can try tar.gz format. If you using windows can use 7 zip

If you using Linux, there is tar default command.
Or php syntax http://www.php.net/manual/en/phardata.extractto.php

Both of them will keep the original timestamp.



Related Topics



Leave a reply



Submit