zoukankan      html  css  js  c++  java
  • PHP扩展类ZipArchive实现压缩Zip文件和文件打包下载

     1 <?php
     2 /**
     3  * 关于文件压缩和下载的类
     4  * @author  tycell
     5  * @version 1.0
     6  */
     7 class zip_down{
     8 
     9     protected $file_path;
    10     /**
    11      * 构造函数
    12      * @param [string] $path [传入文件目录]  
    13      */
    14     public function __construct($path){
    15         $this->file_path=$path; //要打包的根目录
    16     }
    17     /**
    18      * 入口调用函数
    19      * @return [type] [以二进制流的形式返回给浏览器下载到本地]
    20      */
    21     public function index(){
    22         $zip=new ZipArchive();
    23         $end_dir=$this->file_path.date('Ymd',time()).'.zip';//定义打包后的包名
    24         $dir=$this->file_path;
    25         if(!is_dir($dir)){
    26             mkdir($dir);
    27         }
    28         if($zip->open($end_dir, ZipArchive::OVERWRITE) === TRUE){ ///ZipArchive::OVERWRITE 如果文件存在则覆盖
    29             $this->addFileToZip($dir, $zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
    30             $zip->close(); 
    31         }
    32         if(!file_exists($end_dir)){   
    33             exit("无法找到文件"); 
    34         }
    35         header("Cache-Control: public"); 
    36         header("Content-Description: File Transfer"); 
    37         header("Content-Type: application/zip"); //zip格式的   
    38         header('Content-disposition: attachment; filename='.basename($end_dir)); //文件名   
    39         header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件    
    40         header('Content-Length:'.filesize($end_dir)); //告诉浏览器,文件大小   
    41         @readfile($end_dir);
    42         $this->delDirAndFile($dir,true);//删除目录和文件
    43         unlink($end_dir);////删除压缩包
    44     }
    45     /**
    46      * 文件压缩函数 需要开启php zip扩展
    47      * @param [string] $path [路径]
    48      * @param [object] $zip  [扩展ZipArchive类对象]
    49      */
    50     protected function addFileToZip($path, $zip){
    51         $handler = opendir($path);
    52         while (($filename=readdir($handler)) !== false) {
    53             if ($filename!= "." && $filename!=".."){
    54                if(!is_dir($filename)){ 
    55                      $zip->addFile($path."/".$filename,$filename); //第二个参数避免将目录打包,可以不加
    56                 }
    59             }
    60         }
    61         @closedir($path);
    62     }
    63     /**
    64      * 删除文件函数 
    65      * @param  [string]  $dir    [文件目录]
    66      * @param  boolean $delDir [是否删除目录]
    67      * @return [type]          [description]
    68      */
    69     protected function delDirAndFile($path,$delDir=true){
    70         $handle=opendir($path);
    71         if($handle){
    72             while(false!==($item = readdir($handle))){
    73                 if($item!="."&&$item!=".."){
    74                     if(is_dir($path.'/'.$item)){
    75                         $this->delDirAndFile($path.'/'.$item, $delDir);
    76                     }else{
    77                         unlink($path.'/'.$item);
    78                     }
    79                 }
    80             }
    81             @closedir($handle);
    82             if($delDir){return rmdir($path);}
    83         }else{
    84             if(file_exists($path)){
    85                 return unlink($path);
    86             }else{
    87                 return FALSE;
    88             }
    89         }
    90     }
    91 
    92 }
     


  • 相关阅读:
    STL vector的介绍(1)
    linux入门基础——linux软件管理RPM
    Use Local Or Global Index?
    AIX下RAC搭建 Oracle10G(五)安装oracle、建立监听
    HLJU 1220: AC100天 (枚举)
    【JAVA】java中Future、FutureTask的使用
    多线程之线程初始
    微信公众平台开发(85) 获取用户地理位置
    微信公众平台开发(86) 获取用户基本信息
    微信公众平台开发(87) 获取关注者列表
  • 原文地址:https://www.cnblogs.com/tycellwu/p/6764386.html
Copyright © 2011-2022 走看看