ZipArchive 打包下载压缩包

用php的header()方式下载压缩包。

要点:1、不能在header导出压缩包前向浏览器输出内容,否则文件下载压缩包成功,打开的压缩包也会显示被破坏。

         2、在压缩文件包的php代码前不可以有js脚本、meta的加载,否则解压会显示损坏。

 

  1 <?php 
  2 if(isset($_POST['mac']))
  3 {
  4      // print_r($_POST);
  5 
  6      $mac = $_POST['mac'];
  7      $date = $_POST['date'];
  8      $version = $_POST['version'];
  9 
 10      $mac = preg_replace("/[:-]/","", $mac);
 11      $version = preg_replace("/\./","",$version);
 12     
 13     //获取文件列表
 14     function list_dir($dir){
 15             $result = array();
 16             if (is_dir($dir)){
 17                 $file_dir = scandir($dir);
 18                 foreach($file_dir as $file){
 19                     if ($file == '.' || $file == '..'){
 20                         continue;
 21                     }
 22                     elseif (is_dir($dir."/".$file)){
 23                         $result = array_merge($result, list_dir($dir."/".$file));
 24                     }
 25                     else{
 26                         array_push($result, $dir."/".$file);
 27                     }
 28                 }
 29             }
 30             return $result;
 31         }
 32 
 33     //获取列表 
 34     $datalist=list_dir('./package');
 35     // print_r($datalist);
 36     // die();
 37     $filename = "JD_".$mac."_".$version."_".$date.".zip"; //最终生成的文件名(含路径)   
 38     if(!file_exists($filename)){   
 39          //重新生成文件   
 40         $zip = new ZipArchive();//使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释   
 41         if ($zip->open($filename, ZIPARCHIVE::OVERWRITE)!==TRUE) {   
 42             exit('无法打开文件,或者文件创建失败');
 43         }   
 44 
 45         foreach( $datalist as $val){   
 46             if(file_exists($val)){   
 47 
 48                 $zip->addFile($val, basename($val));//第二个参数是放在压缩包中的文件名称,如果文件可能会有重复,就需要注意一下   
 49             }   
 50         }   
 51         $zip->close();//关闭   
 52     }   
 53 
 54     //即使创建,仍有可能失败。。。。   
 55     if(!file_exists($filename)){   
 56         exit("无法找到文件"); 
 57     }   
 58    
 59 
 60 
 61   header("Cache-Control: public"); 
 62   header("Content-Description: File Transfer"); 
 63   header('Content-disposition: attachment; filename='.basename($filename)); //文件名   
 64   header("Content-Type: application/zip"); //zip格式的   
 65   header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件    
 66   header('Content-Length: '. filesize($filename)); //告诉浏览器,文件大小   
 67   @readfile($filename);
 68 
 69 
 70 }
 71  
 72 
 73 
 74   // echo $lowmd5;
 75 ?>
 76 
 77 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
 78     <title>文件打包</title>
 79 <link rel='icon' href='favicon.ico' type='image/x-ico'/> 
 80 <script src="extlib/jquery-1.11.1.min.js" type="text/javascript" charset="utf-8" ></script>
 81 <script src="extlib/bootstrap-3.3.0/dist/js/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>
 82 <script src="extlib/bootstrapValidator.min.js" type="text/javascript" charset="utf-8" ></script>
 83 <!-- <scrip src="extlib/Angular.js" type="text/javascript" charset="utf-8"></script> -->
 84 <script src="http://cdn.bootcss.com/angular.js/1.2.15/angular.min.js"></script>
 85 <link rel="stylesheet" type="text/css" href="extlib/bootstrap-3.3.0/dist/css/bootstrap.min.css">
 86 
 87  
 88  <div style="width:50%;margin:10 auto">
 89  <!-- <div class="container"> -->
 90    <form  name="myForm" class="form-horizontal" method="post" action="filemd5.php" target="_self" ng-app="myApp" ng-controller="validateCtrl" novalidate>
 91        <div class="form-group">
 92            <label for="" class="col-sm-4 control-label">MAC地址:</label>
 93            <div class="col-sm-4">   
 94            <input type="text" class="form-control"  name="mac" value="" ng-model="mac" placeholder="MAC地址"  ng-pattern="/^([A-Za-z0-9]{2}[:-]){5}([A-Za-z0-9]{2})$/" required> 
 95            </div>
 96 
 97            <span ng-show="myForm.mac.$dirty && myForm.mac.$invalid && !myForm.mac.$error.required">mac地址格式错误</span>
 98            <span ng-show="myForm.mac.$error.required && myForm.mac.$dirty">该项为必填项</span>
 99        
100        </div>
101        <div class="form-group">
102            <label for="" class="col-sm-4 control-label">日期:</label>
103            <div class="col-sm-4">
104            <input type="text" class="form-control" name="date" ng-model="date"  value="" ng-pattern="/^\d{8}$/" placeholder="日期" required>
105            </div>
106            <span ng-show="myForm.date.$dirty && myForm.date.$invalid && !myForm.date.$error.required" nowrap>日期格式不正确.YYYYMMDD</span>
107            <span ng-show="myForm.date.$error.required && myForm.date.$dirty">该项为必填项</span> 
108        </div>
109         
110         <div class="form-group">
111             <label for="" class="col-sm-4 control-label">版本号:</label>
112             <div class="col-sm-4">
113                 <input type="text" class="form-control" name="version" ng-model="version" value="" ng-pattern="/^V\d{1,2}.\d{1,2}.\d{1,2}$/" placeholder="版本号" required>
114             </div>
115             <span ng-show="myForm.version.$error.required && myForm.version.$dirty">该项为必填项</span>
116             <span ng-show="myForm.version.$dirty && myForm.version.$invalid && !myForm.version.$error.required">版本号错误.V2.0.03</span>
117         </div>
118 
119       <div class="form-group">   
120          <div class="col-sm-offset-4 col-sm-2">
121           <button type="submit" class="btn btn-default" ng-disabled="myForm.$invalid">生成</button>
122          </div>
123         <div class="col-sm-3">    
124          <button type="reset" class="btn btn-default">重新填写</button>
125         </div>
126 
127       </div>
128   
129   <?
130     if($tag=="success")
131     {
132         echo "<div class='form-group'>";
133         echo "<div id='download' class='col-sm-offset-8'>";
134         echo "<a class=\"btn btn-info\" href=\"$filename\" role=\"button\" ng-show=\"menuState\" ng-click=\"toggleMenu()\">点击下载</a>";
135         echo "</div>";
136         echo "</div>";
137 
138     }
139         
140     
141  ?>
142 
143    </form>
144    <!-- </div> -->
145  </div>
146 <script type="text/javascript">
147     var myApp = angular.module('myApp',[]);
148 
149     myApp.controller('validateCtrl',function($scope){
150 
151       $scope.menuState = true;
152 
153       $scope.toggleMenu=function(){
154 
155            $scope.menuState = false;
156 
157       }
158 
159     })
160 
166 </script>
167 
168 </body>
169 </html>

 

posted @ 2015-09-06 14:25  cogitoergosum  阅读(419)  评论(0编辑  收藏  举报