I am using ZipArchive
to extract files from ZIP.
Here is the code I am using
$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
$zip->extractTo('test/');
$zip->close();
}
It works fine but the last modified date of extracted files changes to current time.
How I can keep the original last modified date of the extracted files?
I improved Badal's answer to apply to all the files in the ZIP (directories will still have the current timestamp):
$res = $zip->open($filename);
if($res === true) {
$zip->extractTo($dataDir);
for($i=0; $i<$zip->numFiles; $i++){
touch($dataDir . $zip->statIndex($i)['name'], $zip->statIndex($i)['mtime']);
}
$zip->close();
}
$dataDir
needs to end with a slash.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With