If your PHP environment does not have the ZipArchive extension installed, you may consider using a third-party PHP compression/decompression library, such as PclZip.
PclZip is a pure PHP-based ZIP compression and decompression library that is easy to use. You simply need to download PclZip and then include its library file in your code:
require_once('path/to/pclzip.lib.php');PclZip is an open-source PHP library for handling compressed files, which can be used to create, read, and decompress ZIP archives. You can download it from the official PclZip website. http://www.phpconcept.net/pclzip/ Download the latest version of the codebase.
After downloading PclZip, you need to extract it into your project directory, then proceed. include perhaps require Command to load a file from the library.
download:
composer require pclzip/pclzipFunction Example
function downloadAndExtract($installDir, $remoteZipUrl) {
// 下载远程 Zip 文件并保存到本地
$tempZip = tempnam(sys_get_temp_dir(), 'download_');
$fp = fopen($tempZip, 'w');
$ch = curl_init($remoteZipUrl);
curl_setopt($ch, CURLOPT_FILE, $fp);
$success = curl_exec($ch);
curl_close($ch);
fclose($fp);
if (!$success) {
throw new Exception('Failed to download the remote ZIP file.');
}
// 解压缩文件到指定目录
$zip = new PclZip($tempZip);
if ($zip->extract(PCLZIP_OPT_PATH, $installDir) === 0) {
throw new Exception('Failed to extract the ZIP file.');
}
// 删除临时文件和 install 目录下的所有文件
unlink($tempZip);
$files = glob($installDir . '/*');
foreach ($files as $file) {
if (is_file($file)) {
unlink($file);
}
}
}