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

StackOverflow https://stackoverflow.com/questions/23600226

  •  20-07-2023
  •  | 
  •  

質問

I have a zip folder on my server containing a complex file structure and hundreds of files all with different timestamps.

I've been using php's ZipArchive to extract everything I need and it's working great apart from all the timestamps once extracted are the current date/time.

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.

他のヒント

I didn't find a way to do what I wanted with a zip folder.

Instead I created a .tar.gz file and used Archive_Tar class to extract the folders and files. To use Archive_Tar the server needs to be running PHP 5.3+ and have PEAR installed.

require_once 'Archive/Tar.php'; 

$path = '';
$import_file_path = $path."/report_import.tar.gz";

if(file_exists($import_file_path)){

    $tar = new Archive_Tar($import_file_path, true);
    $result = $tar->extract($path);
}

There isn't a direct way via the PHP ZipArchive library to extract files while retaining the original timestamps in the archive. However, each file in the archive's last modified timestamp is readable from the archive by reading the 'mtime' variable returned by calling ZipArchive::statName(). You could store the result of this for each file and then manually setting the timestamp of the extracted file on the filesystem's using PHP's touch() function.

You'd probably be better off calling unzip or another tool using exec(), which retains the timestamps by default or with a simple option parameter.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top