Extract zip files in PHP 7.4
1. Reference:https://www.php.net/manual/zh/ziparchive.extractto.php. ZipArchive::ExtractTo. Extract the compressed file to the specified directory. as shown in Figure 1
<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
$zip->extractTo('/my/destination/dir/');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>
2. The final reference implementation is as follows
// 解压缩至的本地目标路径
$destination = storage_path('app') . $directory . '/' . microtime(true) . '.' . mt_rand();
$absolutePath = $destination . '.zip';
app(ZipHandler::class)->unzip($destination, $absolutePath);
/**
* 解压缩 ZIP
* @param string $destination 解压缩至的本地目标路径
* @param string $entrie 待解压缩的文件的绝对路径
* @return void
*/
public function unzip($destination, $entrie) {
$zip = new \ZipArchive;
if ($zip->open($entrie) === TRUE) {
$zip->extractTo($destination);
$zip->close();
} else {
abort(500, 'Unzip failed.');
}
}
3. The decompressed directory structure is the same as the original file name and the same name. as shown in Figure 2

