Hongmu Notes
Home Language Notes PclZip serves as a replacement for ZipArchive, resolving PHP error issues!
Language Notes PHP

PclZip serves as a replacement for ZipArchive, resolving PHP error issues!

PclZip serves as a replacement for ZipArchive, resolving PHP error issues!

PclZip is a PHP-based ZIP archive library that allows you to easily create, read, and extract ZIP files. Unlike the ZipArchive class, PclZip does not require the PHP Zip extension. After spending an entire afternoon experimenting, I ultimately decided to give up on ZipArchive – I really had to give up on it! The original requirement was: <PHP script for remotely downloading a ZIP file, extracting it, and overwriting files into a specified directory; if the specified directory does not exist, it will be created; if the directory already exists, it will be emptied. The extracted ZIP file contains files with Chinese filenames.

由于PclZip官方已经跑路了,可能跑路了吧,我去找的时候,反正已经没了,所以这里准备了一个找到的PclZip包,留着备用一下,其实也就是一个PHP文件!
将里面的函数替换为以下,即可:

 隐藏内容:登录后可查看
function downloadAndExtractZip($remoteZipUrl, $localDir) {
  // 如果本地目录不存在,创建目录
  if (!file_exists($localDir)) {
    mkdir($localDir, 0777, true);
  }

  // 如果本地目录存在,清空目录
  if (file_exists($localDir) && is_dir($localDir)) {
    $files = glob($localDir . '/*'); // 获取目录下的所有文件
    foreach ($files as $file) {
      if (is_file($file)) {
        unlink($file); // 删除文件
      }
    }
  }

  // 下载zip文件到本地临时文件
  $tempFile = tempnam(sys_get_temp_dir(), 'zip');
  $fp = fopen($tempFile, 'w');
  $ch = curl_init($remoteZipUrl);
  curl_setopt($ch, CURLOPT_FILE, $fp);
  curl_exec($ch);
  curl_close($ch);
  fclose($fp);

  // 解压zip文件到本地目录
  $zip = new PclZip($tempFile);
  $extractedFiles = $zip->extract(PCLZIP_OPT_PATH, $localDir, PCLZIP_OPT_SET_CHMOD, 0777);
  if ($extractedFiles != 0) {
    unlink($tempFile); // 删除本地临时文件
    return true;
  } else {
    unlink($tempFile); // 删除本地临时文件
    return false;
  }
}

Download address
☁️
Download #1
Baidu Skydisk
Download now
微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP ob function record

PHP ob function record Language Notes PHP

Usage of the following three functions ob_get_contents(); ob_end_clean(); ob_start(); You can use these functions to buffer local files and execute local script code. Use ob_start() to save the output code into the buffer, and the page will not be displayed; then use ob_get_contents to get the data in the buffer. o…
👁 141
PHP preg

PHP preg Language Notes PHP

The preg_match_all function is used to perform a global regular expression match. preg_match_all() Syntax int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG…
👁 169
Detailed explanation of PHP ternary operator and if

Detailed explanation of PHP ternary operator and if Language Notes PHP

Ternary operator condition ? Result 1 : Result 2 Explanation: The position in front of the question mark is the condition for judgment. If the condition is met, the result is 1, and if it is not met, the result is 2. This article compares and explains the ternary operator and if...else... in detail. I hope it will be helpful to everyone. Today when I was revising my paper online, I encountered a statement that I couldn’t understand: $if_summary = $row['IF_SUMMARY']=…
👁 189
PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232

Recommended reading

Marketing-oriented home decoration paint materials website template – 0898

Marketing-oriented home decoration paint materials website template – 0898 Practical Collection Yiyou template

This EyouCMS template is designed for the marketing-oriented home decoration and paint materials industry, featuring a home decoration marketing design style that effectively showcases paint products, home decoration solutions, color schemes, and brand stories. It enables paint brands to showcase their products online and attract both renovation companies and homeowners. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 50
(Adaptive Mobile Version) Medical Device Website Template | Scientific Instrumentation Website Source Code – 1072

(Adaptive Mobile Version) Medical Device Website Template | Scientific Instrumentation Website Source Code – 1072 Practical Collection pbootcms Template

A PbootCMS website template designed for medical devices and scientific instruments, compatible with both PC and WAP devices. Its professional, tech-oriented design is ideal for showcasing medical equipment, scientific instruments, and technical expertise. This template helps medical and scientific instrumentation companies present their products online and attract potential customers. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 37
(PC+WAP) Blue Steel Structure Mechanical Hardware Website – pbootCMS Template; Download Source Code for Marketing-Oriented Engineering, Construction and Infrastructure Websites – 0115

(PC+WAP) Blue Steel Structure Mechanical Hardware Website – pbootCMS Template; Download Source Code for Marketing-Oriented Engineering, Construction and Infrastructure Websites – 0115 Practical Collection pbootcms Template

This PbootCMS template features a blue color scheme and is ideal for the steel structure, mechanical hardware, and engineering/infrastructure construction industries; it supports both PC and WAP devices. Its robust and professional design effectively showcases steel structure projects and hardware products, making it an excellent choice for infrastructure construction or steel structure enterprises to showcase their brand strength and project portfolio online. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin...
👁 33
Responsive kitchenware website template – 0899

Responsive kitchenware website template – 0899 Practical Collection Yiyou template

An EyouCMS responsive website template designed for the kitchenware industry. Its modern and practical design is ideal for showcasing kitchen products, kitchen accessories, brand stories, and spatial arrangements. This template helps kitchenware brands showcase their products online and attract both household and commercial customers. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 57
(PC+WAP) Constant Temperature Incubator Website Template – Constant Humidity Chamber Website Source Code Download (1073)

(PC+WAP) Constant Temperature Incubator Website Template – Constant Humidity Chamber Website Source Code Download (1073) Practical Collection pbootcms Template

A temperature-controlled incubator and constant humidity chamber website template (PbootCMS), compatible with both PC and WAP devices. Featuring a professional, tech-oriented design ideal for showcasing laboratory equipment such as constant temperature and humidity chambers and incubators. This template helps scientific instrument manufacturers showcase their products online and attract research and testing clients. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 42
(PC + WAP) Vegetable and Fruit Delivery Website – PBootCMS Template; Vegetable and Fruit Production Base Website Source Code Download – 0116

(PC + WAP) Vegetable and Fruit Delivery Website – PBootCMS Template; Vegetable and Fruit Production Base Website Source Code Download – 0116 Practical Collection pbootcms Template

A PbootCMS website template designed for vegetable and fruit distribution services as well as agricultural production bases, supporting access on both PC and WAP devices. Its fresh, green design effectively showcases the freshness of agricultural products and the quality of delivery services. The template includes built-in product display and ordering consultation modules, helping agricultural cooperatives or fresh produce distribution companies expand their online business. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin...
👁 49