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 getextra
field data from local file header yourself usingheader_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
Add One Month to a Given Date (Rounded Day After) With Python
How to Truncate the Time on a Datetime Object
Convert Pandas Dataframe to Numpy Array
Image.Open() Cannot Identify Image File - Python
How to Save a Numpy Array as a 16-Bit Image Using "Normal" (Enthought) Python
Permission System for Discord.Py Bot
Find Similar List Value Inside Dictionary
Python Overflowerror: Int Too Large to Convert to Float
Remove Very First Row in Pandas
How to Use Anaconda Python to Execute a .Py File
How to Dynamically Build a Json Object
How to Write Multiple Images (Subplots) into One Image
Pyspark Replace All Values in Dataframe With Another Values
Cv2.Videocapture.Open() Always Returns False
How to Append Data Using Openpyxl Python to Excel File from a Specified Row
How to Center a Window on the Screen in Tkinter
How to Rotate Xticks on One Axis of Figure in Matplotlib Without "Getting" the Labels as a List