1 用php自带ZipArchive类
public function test(){ $export_dir = "../web"; $export_name = "../test.zip"; $zip = new \ZipArchive(); if(!$zip->open($export_name,\ZIPARCHIVE::CREATE)) { echo "创建[$export_name.zip]失败<br/>";return; } $this->createZip($export_dir,$zip); $zip->close(); }
调用自己写的压缩方法
public function createZip($dir,$zipObj) { $dir_source = opendir($dir); while(($file = readdir($dir_source)) != false) { if($file=="." || $file=="..") continue; $sub_file = $dir.'/'.$file; if(is_dir($sub_file)) { $zipObj->addEmptyDir($sub_file); $this->createZip($sub_file,$zipObj); } if(is_file($sub_file)) { $zipObj->addFile($sub_file); } } }
亲测有效 可以递归压缩子文件夹
2 。用php的exec执行linux zip命令
exec('zip -r ../../web.zip ../web');die;
前面的是你压缩后的地址 。第二个地址是你要压缩的目标文件或文件夹