zoukankan      html  css  js  c++  java
  • PHP 压缩单个或多个文件成ZIP 函数(修复)

    php压缩ZIpphp压缩多个文件
    1. /*  @creates a compressed zip file  将多个文件压缩成一个zip文件的函数  
    2. *   @$files 数组类型  实例array("1.jpg","2.jpg");    
    3. *   @destination  目标文件的路径  如"c:/androidyue.zip"  
    4. *   @$overwrite 是否为覆盖与目标文件相同的文件  
    5. *   @Recorded By Androidyue  
    6. *   此处的basename是其真实的文件名;如果不用它,你的文件会被包含在诸如:./var/www/upload 目录下 很难找;同时打包后的压缩文件 必须重新命名(在header中操作);否则就只能呵呵了,暴露根目录...
    7. *   @Blog:http://thinkblog.sinaapp.com  
    8.  */    
    9. function create_zip($files = array(),$destination = '',$overwrite = false) {    
    10.     //if the zip file already exists and overwrite is false, return false    
    11.     //如果zip文件已经存在并且设置为不重写返回false    
    12.     if(file_exists($destination) && !$overwrite) { return false; }    
    13.     //vars    
    14.     $valid_files = array();    
    15.     //if files were passed in...    
    16.     //获取到真实有效的文件名    
    17.     if(is_array($files)) {    
    18.         //cycle through each file    
    19.         foreach($files as $file) {    
    20.         //make sure the file exists    
    21.             if(file_exists($file)) {    
    22.             $valid_files[] = $file;    
    23.             }    
    24.         }    
    25.     }    
    26.     //if we have good files...    
    27.     //如果存在真实有效的文件    
    28.     if(count($valid_files)) {    
    29.         //create the archive    
    30.         $zip = new ZipArchive();    
    31.         //打开文件       如果文件已经存在则覆盖,如果没有则创建    
    32.         if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {    
    33.             return false;    
    34.         }    
    35.         //add the files    
    36.         //向压缩文件中添加文件    
    37.         foreach($valid_files as $file) {   
    38.             $file_info_arrpathinfo($file);  
    39.         $filename =$file_info_arr['basename'];   
    40.                 $zip->addFile($file,$filename);    
    41.         }    
    42.         //debug    
    43.         //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;    
    44.         //close the zip -- done!    
    45.         //关闭文件    
    46.         $zip->close();    
    47.         //check to make sure the file exists    
    48.         //检测文件是否存在    
    49.         return file_exists($destination);    
    50.     }else{    
    51.         //如果没有真实有效的文件返回false    
    52.         return false;    
    53.     }    
    54. }    
    1. 原函数中://向压缩文件中添加文件    
    2.         foreach($valid_files as $file) {   
    3.             $zip->addFile($file,$file);    
    4.         }   
    5. 此处经过测试存在bug,压缩后的zip中包含了zip所在的目录结构,所以修改成:  
    6. //向压缩文件中添加文件    
    7.         foreach($valid_files as $file) {   
    8.             $file_info_arrpathinfo($file);  
    9.          $filename =$file_info_arr['basename'];   
    10.                 $zip->addFile($file,$filename);    
    11.         }   
  • 相关阅读:
    去除测序reads中的接头:adaptor
    Python学习_13_继承和元类
    Matplotlib初体验
    Python学习_12_方法和类定制
    python+requests接口自动化测试框架实例详解教程
    typeof与instanceof运算符
    闭包与递归函数的区别
    添加SSH
    AndroidStudio常用快捷键总结
    git新建分支
  • 原文地址:https://www.cnblogs.com/sunscheung/p/4839438.html
Copyright © 2011-2022 走看看