php 文件压缩
PclZip文件压缩实现(推荐)我在做项目是的时候有个打包下载的需求:把上传的多个文件压缩成一个文件并下载,我用的比较强大的PclZip类实现的。我的用的是thinkphp3.2框架开发的,具体实现代码如下
/**
* 压缩文件
* @param $zipName压缩的文件名
* @param $fileName 要压缩的文件路径的数组或字符串
*@param$savePath要保存的路径
* @return bool
*/
public function createZip($zipName,$fileName,$savePath){
//加载类
import("Org.Io.PclZip");
$archive = new \PclZip($zipName);
$list = $archive->create($fileName,PCLZIP_OPT_REMOVE_ALL_PATH); //压缩文件
if($list == 0){
return false;
}
//创建目录,不存在就创建
if( !is_dir( $savePath)) {
mkdir( $savePath);
}
//检查文件是否存在
if(!file_exists($zipName)){
return false;
}
//移动文件 create方法不能指定压缩文件保存路径,默认是保存在当前目录。
rename ($zipName,$savePath.$zipName);
return $savePath.$zipName;
} 其他参考资料:http://blog.csdn.net/gumanren/article/details/5520173
其他实现方式
1.PHP自带ZIP压缩、解压缩类ZipArchiv使用指南:
http://www.iyunv.net/article/61678.htm
http://www.cnblogs.com/keleyu/p/4273120.html
2.PHP压缩与解压Zip(PHPZip类)
http://www.oschina.net/code/snippet_167936_6898
页:
[1]