找回密码
 新建账号

[PHP] PHP压缩文件夹和子目录为zip文件 使用内置ZipArchive类实现

[复制链接]
php 发表于 2015/12/17 02:53 | 显示全部楼层 |阅读模式
PHP ZipArchive 压缩目录及子目录所有文件为 ZIP 压缩包文件,使用 PHP 核心内置的 ZipArchive + RecursiveIteratorIterator + RecursiveDirectoryIterator 实现 ZipArchive 压缩文件夹及下级文件夹中的全部文件为ZIP压缩包。
PHP ZipArchive 类需要 php_zip.dll 扩展支持,从 PHP 5.3 开始已经内置在 PHP 核心中,而且默认可用。
PHP 压缩文件夹函数为 ZIP 文件的代码及示例。如果文件名包含中文等当前 PHP 版本不支持的字符,请自行转码。PHP 7.1+ 支持 UTF-8 文件名和 I/O 流,不再需要转码。建议使用最高版本的 PHP 以免遇到不必要的麻烦。
  1. <?php
  2.         /**
  3.          * PHP ZipArchive压缩文件夹,实现将目录及子目录中的所有文件压缩为zip文件
  4.          * @author 吴先成 wuxiancheng.cn 高阶代码 原创发布
  5.          * @param string $folderPath 要压缩的目录路径 绝对路径和相对路径都可以
  6.          * @param string $zipAs 压缩包文件的文件名,可以带路径,不能为空
  7.          * @return bool 成功时返回true,否则返回false
  8.          */
  9.         function zipFolder($folderPath, $zipAs){
  10.                 if(!is_scalar($folderPath) || !is_scalar($zipAs)){
  11.                         return false;
  12.                 }
  13.                 $folderPath = (string)$folderPath;
  14.                 $folderPath = str_replace('\\', '/', $folderPath);               
  15.                 $zipAs = (string)$zipAs;
  16.                 if($zipAs === ''){
  17.                         return false;
  18.                 }
  19.                 try{
  20.                         $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($folderPath, RecursiveDirectoryIterator::UNIX_PATHS|RecursiveDirectoryIterator::CURRENT_AS_SELF|RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST, RecursiveIteratorIterator::CATCH_GET_CHILD);
  21.                         $zipObject   = new ZipArchive();
  22.                         $errorCode = $zipObject->open($zipAs, ZipArchive::CREATE|ZipArchive::OVERWRITE);
  23.                         if($errorCode !== true){
  24.                                 return false;
  25.                         }
  26.                         foreach($files as $file){
  27.                                 $subPath = $file->getSubPathname();
  28.                                 if($file->isDir()){
  29.                                         $subPath = rtrim($subPath, '/') . '/';
  30.                                         $zipObject->addEmptyDir($subPath);
  31.                                 }else{
  32.                                         $zipObject->addFile($subPath);
  33.                                 }
  34.                         }
  35.                         if($zipObject->close()){
  36.                                 return true;
  37.                         }
  38.                 }catch(Exception $e){                        
  39.                 }
  40.                 return false;
  41.         }
  42.         
  43.         /* 语法举例 */
  44.         if(zipFolder(getcwd(), 'wuxiancheng.cn.backup.zip')){
  45.                 echo 'success';
  46.         }else{
  47.                 echo 'failure';
  48.         }
  49. ?>
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?新建账号

×
neverchange2007 发表于 2016/5/30 22:13 | 显示全部楼层
子目录为zip文件 使用内置ZipArchive类实现

手机版|轻松E站

GMT+8, 2024/4/19 17:32

快速回复 返回顶部 返回列表