PHP生成zip压缩包的常用方法示例

2019-11-13,,,

本文实例讲述了PHP生成zip压缩包的常用方法。分享给大家供大家参考,具体如下:

压缩一个文件

我们将一个文件生成一个压缩包。

<?php
$path = "c:/wamp/www/log.txt";
$filename = "test.zip";
$zip = new ZipArchive();
$zip->open($filename,ZipArchive::CREATE);  //打开压缩包
$zip->addFile($path,basename($path));  //向压缩包中添加文件
$zip->close(); //关闭压缩包

上述代码将c:/wamp/www/log.txt文件压缩生成了test.zip,并保存在当前目录。

压缩多个文件

压缩多个文件,其实就是addFile执行多次,可以通过数组的遍历来实现。

<?php
$fileList = array(
  "c:/wamp/www/log.txt",
  "c:/wamp/www/weixin.class.php"
);
$filename = "test.zip";
$zip = new ZipArchive();
$zip->open($filename,ZipArchive::CREATE);  //打开压缩包
foreach($fileList as $file){
  $zip->addFile($file,basename($file));  //向压缩包中添加文件
}
$zip->close(); //关闭压缩包

压缩一个目录

<?php
function addFileToZip($path,$zip){
  $handler=opendir($path); //打开当前文件夹由$path指定。
  while(($filename=readdir($handler))!==false){
    if($filename != "." && $filename != ".."){//文件夹文件名字为'.'和‘..',不要对他们进行操作
      if(is_dir($path."/".$filename)){// 如果读取的某个对象是文件夹,则递归
        addFileToZip($path."/".$filename, $zip);
      }else{ //将文件加入zip对象
        $zip->addFile($path."/".$filename);
      }
    }
  }
  @closedir($path);
}
$zip=new ZipArchive();
if($zip->open('rsa.zip', ZipArchive::OVERWRITE)=== TRUE){
  addFileToZip('rsa/', $zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
  $zip->close(); //关闭处理的zip文件
}

压缩并下载zip包

我的时候,我们需要打包之后,提供下载,然后删除压缩包。

可以分为以下几步:

  1. 判断给出的路径,是文件夹,还是文件。文件夹还需要遍历添加文件。
  2. 设置相关文件头,并使用readfile函数提供下载。
  3. 使用unlink函数删除压缩包
<?php
function addFileToZip($path,$zip){
  $handler=opendir($path); //打开当前文件夹由$path指定。
  while(($filename=readdir($handler))!==false){
    if($filename != "." && $filename != ".."){//文件夹文件名字为'.'和‘..',不要对他们进行操作
      if(is_dir($path."/".$filename)){// 如果读取的某个对象是文件夹,则递归
        addFileToZip($path."/".$filename, $zip);
      }else{ //将文件加入zip对象
        $zip->addFile($path."/".$filename);
      }
    }
  }
  @closedir($path);
}
$zip=new ZipArchive();
if($zip->open('rsa.zip', ZipArchive::OVERWRITE)=== TRUE){
  $path = 'rsa/';
  if(is_dir($path)){ //给出文件夹,打包文件夹
    addFileToZip($path, $zip);
  }else if(is_array($path)){ //以数组形式给出文件路径
    foreach($path as $file){
      $zip->addFile($file);
    }
  }else{   //只给出一个文件
    $zip->addFile($path);
  }
  $zip->close(); //关闭处理的zip文件
}

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP操作zip文件及压缩技巧总结》、《php文件操作总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

《PHP生成zip压缩包的常用方法示例.doc》

下载本文的Word格式文档,以方便收藏与打印。