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;//新建一個ZipArchive的對象
- /*
- 通過ZipArchive的對象處理zip文件
- $zip->open這個方法的參數表示處理的zip文件名。
- 如果對zip文件對象操作成功,$zip->open這個方法會返回TRUE
- */
- if ($zip->open('test.zip') === TRUE)
- {//開源代碼Vevb.com
- $zip->extractTo('images');//假設解壓縮到在當前路徑下images文件夾的子文件夾php
- $zip->close();//關閉處理的zip文件
- }
二、將文件壓縮成zip文件,代碼如下:
- $zip = new ZipArchive;
- /*
- $zip->open這個方法第一個參數表示處理的zip文件名。
- 第二個參數表示處理模式,ZipArchive::OVERWRITE表示如果zip文件存在,就覆蓋掉原來的zip文件。
- 如果參數使用ZIPARCHIVE::CREATE,系統就會往原來的zip文件里添加內容。
- 如果不是為了多次添加內容到zip文件,建議使用ZipArchive::OVERWRITE。
- 使用這兩個參數,如果zip文件不存在,系統都會自動新建。
- 如果對zip文件對象操作成功,$zip->open這個方法會返回TRUE
- */
- if ($zip->open('test.zip', ZipArchive::OVERWRITE) === TRUE)
- {
- $zip->addFile('image.txt');//假設加入的文件名是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();//開源代碼Vevb.com
- echo 'ok';
- } else {
- echo 'failed';
- }
例子,執行打包代碼如下:
- import('ORG.Util.FileToZip');
- // 打包下載
- $cur_file =getcwd().'/dimg/2014052916/';
- $handler = opendir($cur_file); //$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); //$save_path zip包文件目錄
- $scandir->tozip($download_file);
FileToZip 類,代碼如下:
- <?php
- /**
- * 遍歷目錄,打包成zip格式
- */
- 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>";
- }
- }
- /**
- * 返回文件的大小
- *
- * @param string $filename 文件名
- * @return 文件大小(KB)
- */
- public function getfilesize($fname){
- return filesize($fname)/1024;
- }
- /**
- * 壓縮文件(zip格式)
- */
- public function tozip($items){
- $zip=new ZipArchive();
- $zipname=date('YmdHis',time());
- if (!file_exists($zipname)){
- $zip->open($savepath.$zipname.'.zip',ZipArchive::OVERWRITE);//創建一個空的zip文件
- 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));
- //修改之后,一次只傳輸1024個字節的數據給客戶端
- //向客戶端回送數據
- $buffer=1024;//
- //判斷文件是否讀完
- while (!feof($file)) {
- //將文件讀入內存
- $file_data=fread($file,$buffer);
- //每次向客戶端回送1024個字節的數據
- echo $file_data;
- }
- fclose($file);
- }else {
- echo "<script>alert('對不起,您要下載的文件不存在');</script>";
- }
- }
- }
- ?>
新聞熱點
疑難解答