php ZipArchive打包压缩zip文件实例
ZipArchive类是一个文件压缩解压类是一个php自来的zip类,我们可以直接简单创建一个类然后就能实现打包了,下面小编给各位介绍一下吧,有需要了解的朋友可进入参考.
这里我采用的是php自带的ZipArchive类
a) 我们只需要new一个ZipArchive对象,然后使用open方法创建一个zip文件,接着使用addFile方法,将要打包的文件写入刚刚创建的zip文件中,最好还得记得关闭该对象。
b) 注意点:使用open方法的时候,第二个参数$flags是可选的,$flags用来指定对打开的zip文件的处理方式,共有四种情况
i.ZIPARCHIVE::OVERWRITE 总是创建一个新的文件,如果指定的zip文件存在,则会覆盖掉
ii. ZIPARCHIVE::CREATE如果指定的zip文件不存在,则新建一个
iii. ZIPARCHIVE::EXCL 如果指定的zip文件存在,则会报错
iv. ZIPARCHIVE::CHECKCONS
一、解压缩zip文件,代码如下:
- $zip = new ZipArchive;
-
-
-
-
-
- if ($zip->open('test.zip') === TRUE)
- {
- $zip->extractTo('images');
- $zip->close();
- }
二、将文件压缩成zip文件,代码如下:
- $zip = new ZipArchive;
-
-
-
-
-
-
-
-
- if ($zip->open('test.zip', ZipArchive::OVERWRITE) === TRUE)
- {
- $zip->addFile('image.txt');
- $zip->close();
- }
三、文件追加内容添加到zip文件,代码如下:
- $zip = new ZipArchive;
- $res = $zip->open('test.zip', ZipArchive::CREATE);
- if ($res === TRUE) {
- $zip->addFromString('test.txt', 'file content goes here');
- $zip->close();
- echo 'ok';
- } else {
- echo 'failed';
- }
例子,执行打包代码如下:
- import('ORG.Util.FileToZip');
-
- $cur_file =getcwd().'/dimg/2014052916/';
- $handler = opendir($cur_file);
- $download_file = array();
- $i = 0;
- while( ($filename = readdir($handler)) !== false ) {
- if($filename != '.' && $filename != '..') {
- $download_file[$i++] = $filename;
- }
- }
- closedir($handler);
- $scandir=new traverseDir($cur_file,$save_path);
- $scandir->tozip($download_file);
FileToZip 类,代码如下:
- <?php
-
-
-
- class traverseDir{
- public $currentdir;
- public $filename;
- public $fileinfo;
- public $savepath;
- public function __construct($curpath,$savepath){
- $this->currentdir=$curpath;
- $this->savepath=$savepath;
- }
-
- public function scandir($filepath){
- if (is_dir($filepath)){
- $arr=scandir($filepath);
- foreach ($arr as $k=>$v){
- $this->fileinfo[$v][]=$this->getfilesize($v);
- }
- }else {
- echo "<script>alert('当前目录不是有效目录');</script>";
- }
- }
-
-
-
-
-
-
- public function getfilesize($fname){
- return filesize($fname)/1024;
- }
-
-
-
-
- public function tozip($items){
- $zip=new ZipArchive();
- $zipname=date('YmdHis',time());
- if (!file_exists($zipname)){
- $zip->open($savepath.$zipname.'.zip',ZipArchive::OVERWRITE);
- for ($i=0;$i<count($items);$i++){
- $zip->addFile($this->currentdir.'/'.$items[$i],$items[$i]);
- }
- $zip->close();
- $dw=new download($zipname.'.zip',$savepath);
- $dw->getfiles();
- unlink($savepath.$zipname.'.zip');
- }
- }
- }
-
-
-
-
-
- class download{
- protected $_filename;
- protected $_filepath;
- protected $_filesize;
- protected $savepath;
- public function __construct($filename,$savepath){
- $this->_filename=$filename;
- $this->_filepath=$savepath.$filename;
- }
-
- public function getfilename(){
- return $this->_filename;
- }
-
-
- public function getfilepath(){
- return $this->_filepath;
- }
-
-
- public function getfilesize(){
- return $this->_filesize=number_format(filesize($this->_filepath)/(1024*1024),2);
- }
-
- public function getfiles(){
-
- if (file_exists($this->_filepath)){
-
- $file = fopen($this->_filepath,"r");
-
- Header("Content-type: application/octet-stream");
-
- Header("Accept-Ranges: bytes");
-
- Header("Accept-Length: ".filesize($this->_filepath));
-
- Header("Content-Disposition: attachment; filename=".$this->_filename);
-
- echo fread($file, filesize($this->_filepath));
-
-
- $buffer=1024;
-
- while (!feof($file)) {
-
- $file_data=fread($file,$buffer);
-
- echo $file_data;
- }
-
- fclose($file);
- }else {
- echo "<script>alert('对不起,您要下载的文件不存在');</script>";
- }
- }
- }
- ?>