php上传zip文件在线解压文件在指定目录下,CI框架版本

时间:2022-02-05 11:28:53

我从网上找的文件php在线解压zip压缩文件

文件为jy.php可以直接执行,但是怎样将其加到CI框架中呢??

jy.php文件

php上传zip文件在线解压文件在指定目录下,CI框架版本php上传zip文件在线解压文件在指定目录下,CI框架版本
  1 <?php
  2 header("content-Type: text/html; charset=utf-8");
  3 //验证密码
  4 $password = "123456";
  5 ?>
  6 <html>
  7 <head>
  8 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  9 <title>在线解压ZIP文件程序 V1.0</title>
 10 <style type="text/css">
 11 <!--
 12 body,td{
 13     font-size: 14px;
 14     color: #000000;
 15 }
 16 a {
 17     color: #000066;
 18     text-decoration: none;
 19 }
 20 a:hover {
 21     color: #FF6600;
 22     text-decoration: underline;
 23 }
 24 -->
 25 </style>
 26 </head>
 27 
 28 <body>
 29   <form name="myform" method="post" action="<?=$_SERVER[PHP_SELF];?>" enctype="multipart/form-data" onSubmit="return check_uploadObject(this);">
 30 <?
 31     if(!$_REQUEST["myaction"]):
 32 ?>
 33 
 34 <script language="javascript">
 35 function check_uploadObject(form){
 36     if(form.password.value==''){
 37         alert('请输入密码.');
 38         return false;
 39     }
 40     return true;
 41 }
 42 </script>
 43 
 44 <table width="100%" border="0" cellspacing="0" cellpadding="4">
 45     <tr>
 46       <td height="40" colspan="2" style="color:#FF9900"><p><font color="#FF0000">在线解压ZIP文件程序 V1.0</font></p>
 47       <p>使用方法:把zip文件通过FTP上传到本文件相同的目录下,选择zip文件;或直接点击“浏览...”上传zip文件。</p>
 48 <p>&nbsp;</p>
 49  </td>
 50     </tr>
 51     <tr>
 52       <td width="11%">选择ZIP文件: </td>
 53       <td width="89%"><select name="zipfile">
 54         <option value="" selected>- 请选择 -</option>
 55 <?
 56       $fdir = opendir('./');
 57     while($file=readdir($fdir)){
 58         if(!is_file($file)) continue;
 59         if(preg_match('/\.zip$/mis',$file)){
 60             echo "<option value='$file'>$file</option>\r\n";
 61         }
 62     }
 63 ?>
 64       </select></td>
 65     </tr>
 66     <tr>
 67       <td width="11%" nowrap>或上传文件: </td>
 68       <td width="89%"><input name="upfile" type="file" id="upfile" size="20"></td>
 69     </tr>
 70     <tr>
 71       <td>解压到目录: </td>
 72       <td><input name="todir" type="text" id="todir" value="" size="15"> 
 73       (留空为本目录,必须有写入权限)</td>
 74     </tr>
 75     <tr>
 76       <td>验证密码: </td>
 77       <td><input name="password" type="password" id="password" size="15"> 
 78         (源文件中设定的密码)</td>
 79     </tr>    
 80     <tr>
 81       <td><input name="myaction" type="hidden" id="myaction" value="dounzip"></td>
 82       <td><input type="submit" name="Submit" value=" 解 压 "></td>
 83     </tr>
 84   </table>
 85 
 86 <?
 87 
 88 elseif($_REQUEST["myaction"]=="dounzip"):
 89 
 90 
 91 class zip
 92 {
 93 
 94  var $total_files = 0;
 95  var $total_folders = 0; 
 96 
 97  function Extract ( $zn, $to, $index = Array(-1) )
 98  {
 99    $ok = 0; $zip = @fopen($zn,'rb');
100    if(!$zip) return(-1);
101    $cdir = $this->ReadCentralDir($zip,$zn);
102    $pos_entry = $cdir['offset'];
103 
104    if(!is_array($index)){ $index = array($index);  }
105    for($i=0; $index[$i];$i++){
106            if(intval($index[$i])!=$index[$i]||$index[$i]>$cdir['entries'])
107         return(-1);
108    }
109    for ($i=0; $i<$cdir['entries']; $i++)
110    {
111      @fseek($zip, $pos_entry);
112      $header = $this->ReadCentralFileHeaders($zip);
113      $header['index'] = $i; $pos_entry = ftell($zip);
114      @rewind($zip); fseek($zip, $header['offset']);
115      if(in_array("-1",$index)||in_array($i,$index))
116          $stat[$header['filename']]=$this->ExtractFile($header, $to, $zip);
117    }
118    fclose($zip);
119    return $stat;
120  }
121 
122   function ReadFileHeader($zip)
123   {
124     $binary_data = fread($zip, 30);
125     $data = unpack('vchk/vid/vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $binary_data);
126 
127     $header['filename'] = fread($zip, $data['filename_len']);
128     if ($data['extra_len'] != 0) {
129       $header['extra'] = fread($zip, $data['extra_len']);
130     } else { $header['extra'] = ''; }
131 
132     $header['compression'] = $data['compression'];$header['size'] = $data['size'];
133     $header['compressed_size'] = $data['compressed_size'];
134     $header['crc'] = $data['crc']; $header['flag'] = $data['flag'];
135     $header['mdate'] = $data['mdate'];$header['mtime'] = $data['mtime'];
136 
137     if ($header['mdate'] && $header['mtime']){
138      $hour=($header['mtime']&0xF800)>>11;$minute=($header['mtime']&0x07E0)>>5;
139      $seconde=($header['mtime']&0x001F)*2;$year=(($header['mdate']&0xFE00)>>9)+1980;
140      $month=($header['mdate']&0x01E0)>>5;$day=$header['mdate']&0x001F;
141      $header['mtime'] = mktime($hour, $minute, $seconde, $month, $day, $year);
142     }else{$header['mtime'] = time();}
143 
144     $header['stored_filename'] = $header['filename'];
145     $header['status'] = "ok";
146     return $header;
147   }
148 
149  function ReadCentralFileHeaders($zip){
150     $binary_data = fread($zip, 46);
151     $header = unpack('vchkid/vid/vversion/vversion_extracted/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/Voffset', $binary_data);
152 
153     if ($header['filename_len'] != 0)
154       $header['filename'] = fread($zip,$header['filename_len']);
155     else $header['filename'] = '';
156 
157     if ($header['extra_len'] != 0)
158       $header['extra'] = fread($zip, $header['extra_len']);
159     else $header['extra'] = '';
160 
161     if ($header['comment_len'] != 0)
162       $header['comment'] = fread($zip, $header['comment_len']);
163     else $header['comment'] = '';
164 
165     if ($header['mdate'] && $header['mtime'])
166     {
167       $hour = ($header['mtime'] & 0xF800) >> 11;
168       $minute = ($header['mtime'] & 0x07E0) >> 5;
169       $seconde = ($header['mtime'] & 0x001F)*2;
170       $year = (($header['mdate'] & 0xFE00) >> 9) + 1980;
171       $month = ($header['mdate'] & 0x01E0) >> 5;
172       $day = $header['mdate'] & 0x001F;
173       $header['mtime'] = mktime($hour, $minute, $seconde, $month, $day, $year);
174     } else {
175       $header['mtime'] = time();
176     }
177     $header['stored_filename'] = $header['filename'];
178     $header['status'] = 'ok';
179     if (substr($header['filename'], -1) == '/')
180       $header['external'] = 0x41FF0010;
181     return $header;
182  }
183 
184  function ReadCentralDir($zip,$zip_name){
185     $size = filesize($zip_name);
186 
187     if ($size < 277) $maximum_size = $size;
188     else $maximum_size=277;
189     
190     @fseek($zip, $size-$maximum_size);
191     $pos = ftell($zip); $bytes = 0x00000000;
192     
193     while ($pos < $size){
194         $byte = @fread($zip, 1); $bytes=($bytes << 8) | ord($byte);
195         if ($bytes == 0x504b0506 or $bytes == 0x2e706870504b0506){ $pos++;break;} $pos++;
196     }
197     
198     $fdata=fread($zip,18);
199     
200     $data=@unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size',$fdata);
201     
202     if ($data['comment_size'] != 0) $centd['comment'] = fread($zip, $data['comment_size']);
203     else $centd['comment'] = ''; $centd['entries'] = $data['entries'];
204     $centd['disk_entries'] = $data['disk_entries'];
205     $centd['offset'] = $data['offset'];$centd['disk_start'] = $data['disk_start'];
206     $centd['size'] = $data['size'];  $centd['disk'] = $data['disk'];
207     return $centd;
208   }
209 
210  function ExtractFile($header,$to,$zip){
211     $header = $this->readfileheader($zip);
212     
213     if(substr($to,-1)!="/") $to.="/";
214     if($to=='./') $to = '';    
215     $pth = explode("/",$to.$header['filename']);
216     $mydir = '';
217     for($i=0;$i<count($pth)-1;$i++){
218         if(!$pth[$i]) continue;
219         $mydir .= $pth[$i]."/";
220         if((!is_dir($mydir) && @mkdir($mydir,0777)) || (($mydir==$to.$header['filename'] || ($mydir==$to && $this->total_folders==0)) && is_dir($mydir)) ){
221             @chmod($mydir,0777);
222             $this->total_folders ++;
223             echo "<input name='dfile[]' type='checkbox' value='$mydir' checked> <a href='$mydir' target='_blank'>目录: $mydir</a><br>";
224         }
225     }
226     
227     if(strrchr($header['filename'],'/')=='/') return;    
228 
229     if (!($header['external']==0x41FF0010)&&!($header['external']==16)){
230         if ($header['compression']==0){
231             $fp = @fopen($to.$header['filename'], 'wb');
232             if(!$fp) return(-1);
233             $size = $header['compressed_size'];
234         
235             while ($size != 0){
236                 $read_size = ($size < 2048 ? $size : 2048);
237                 $buffer = fread($zip, $read_size);
238                 $binary_data = pack('a'.$read_size, $buffer);
239                 @fwrite($fp, $binary_data, $read_size);
240                 $size -= $read_size;
241             }
242             fclose($fp);
243             touch($to.$header['filename'], $header['mtime']);
244         }else{
245             $fp = @fopen($to.$header['filename'].'.gz','wb');
246             if(!$fp) return(-1);
247             $binary_data = pack('va1a1Va1a1', 0x8b1f, Chr($header['compression']),
248             Chr(0x00), time(), Chr(0x00), Chr(3));
249             
250             fwrite($fp, $binary_data, 10);
251             $size = $header['compressed_size'];
252         
253             while ($size != 0){
254                 $read_size = ($size < 1024 ? $size : 1024);
255                 $buffer = fread($zip, $read_size);
256                 $binary_data = pack('a'.$read_size, $buffer);
257                 @fwrite($fp, $binary_data, $read_size);
258                 $size -= $read_size;
259             }
260         
261             $binary_data = pack('VV', $header['crc'], $header['size']);
262             fwrite($fp, $binary_data,8); fclose($fp);
263     
264             $gzp = @gzopen($to.$header['filename'].'.gz','rb') or die("Cette archive est compress閑");
265             if(!$gzp) return(-2);
266             $fp = @fopen($to.$header['filename'],'wb');
267             if(!$fp) return(-1);
268             $size = $header['size'];
269         
270             while ($size != 0){
271                 $read_size = ($size < 2048 ? $size : 2048);
272                 $buffer = gzread($gzp, $read_size);
273                 $binary_data = pack('a'.$read_size, $buffer);
274                 @fwrite($fp, $binary_data, $read_size);
275                 $size -= $read_size;
276             }
277             fclose($fp); gzclose($gzp);
278         
279             touch($to.$header['filename'], $header['mtime']);
280             @unlink($to.$header['filename'].'.gz');
281             
282         }
283     }
284     
285     $this->total_files ++;
286     echo "<input name='dfile[]' type='checkbox' value='$to$header[filename]' checked> <a href='$to$header[filename]' target='_blank'>文件: $to$header[filename]</a><br>";
287 
288     return true;
289  }
290 
291 // end class
292 }
293 
294     set_time_limit(0);
295 
296     if ($_POST['password'] != $password) die("输入的密码不正确,请重新输入。");
297     if(!$_POST["todir"]) $_POST["todir"] = ".";
298     $z = new Zip;
299     $have_zip_file = 0;
300     function start_unzip($tmp_name,$new_name,$checked){
301         global $_POST,$z,$have_zip_file;
302         $upfile = array("tmp_name"=>$tmp_name,"name"=>$new_name);
303         if(is_file($upfile[tmp_name])){
304             $have_zip_file = 1;
305             echo "<br>正在解压: <input name='dfile[]' type='checkbox' value='$upfile[name]' ".($checked?"checked":"")."> $upfile[name]<br><br>";
306             if(preg_match('/\.zip$/mis',$upfile[name])){
307                 $result=$z->Extract($upfile[tmp_name],$_POST["todir"]);
308                 if($result==-1){
309                     echo "<br>文件 $upfile[name] 错误.<br>";
310                 }
311                 echo "<br>完成,共建立 $z->total_folders 个目录,$z->total_files 个文件.<br><br><br>";
312             }else{
313                 echo "<br>$upfile[name] 不是 zip 文件.<br><br>";            
314             }
315             if(realpath($upfile[name])!=realpath($upfile[tmp_name])){
316                 @unlink($upfile[name]);
317                 rename($upfile[tmp_name],$upfile[name]);
318             }
319         }
320     }
321     clearstatcache();
322     
323     start_unzip($_POST["zipfile"],$_POST["zipfile"],0);
324     start_unzip($_FILES["upfile"][tmp_name],$_FILES["upfile"][name],1);
325 
326     if(!$have_zip_file){
327         echo "<br>请选择或上传文件.<br>";
328     }
329 ?>
330 <input name="password" type="hidden" id="password" value="<?=$_POST['password'];?>">
331 <input name="myaction" type="hidden" id="myaction" value="dodelete">
332 <input name="按钮" type="button" value="返回" onClick="window.location='<?=$_SERVER[PHP_SELF];?>';">
333 
334 <input type='button' value='反选' onclick='selrev();'> <input type='submit' onclick='return confirm("删除选定文件?");' value='删除选定'>
335 
336 <script language='javascript'>
337 function selrev() {
338     with(document.myform) {
339         for(i=0;i<elements.length;i++) {
340             thiselm = elements[i];
341             if(thiselm.name.match(/dfile\[]/))    thiselm.checked = !thiselm.checked;
342         }
343     }
344 }
345 alert('完成.');
346 </script>
347 <?
348 
349 elseif($_REQUEST["myaction"]=="dodelete"):
350     set_time_limit(0);
351     if ($_POST['password'] != $password) die("输入的密码不正确,请重新输入。");
352     
353     $dfile = $_POST["dfile"]; 
354     echo "正在删除文件...<br><br>";
355     if(is_array($dfile)){
356         for($i=count($dfile)-1;$i>=0;$i--){
357             if(is_file($dfile[$i])){
358                 if(@unlink($dfile[$i])){
359                     echo "已删除文件: $dfile[$i]<br>";
360                 }else{
361                     echo "删除文件失败: $dfile[$i]<br>";
362                 }
363             }else{
364                 if(@rmdir($dfile[$i])){
365                     echo "已删除目录: $dfile[$i]<br>";
366                 }else{
367                     echo "删除目录失败: $dfile[$i]<br>";
368                 }                
369             }
370             
371         }
372     }
373     echo "<br>完成.<br><br><input type='button' value='返回' onclick=\"window.location='$_SERVER[PHP_SELF]';\"><br><br>
374          <script language='javascript'>('完成.');</script>";
375 
376 endif;
377 
378 ?>
379   </form>
380 </html>
View Code

CI框架中我们先建立显示页面

views/informaction_zip.php

php上传zip文件在线解压文件在指定目录下,CI框架版本php上传zip文件在线解压文件在指定目录下,CI框架版本
 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 2        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 3 
 4 <html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">
 5 <!--
 6  * Created on 2013-8-5
 7  *
 8  * To change the template for this generated file go to
 9  * Window - Preferences - PHPeclipse - PHP - Code Templates
10 -->
11  <head>
12  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
13   <title> </title>
14 <style type="text/css">
15 <!--
16 body,td{
17     font-size: 14px;
18     color: #000000;
19 }
20 a {
21     color: #000066;
22     text-decoration: none;
23 }
24 a:hover {
25     color: #FF6600;
26     text-decoration: underline;
27 }
28 -->
29 </style>
30 </head>
31 
32 <body>
33   <form name="myform" method="post" action="<?= base_url() ?>index.php/admin/zip/zipUtil" enctype="multipart/form-data" onSubmit="return check_uploadObject(this);">
34 </script>
35 
36 <table width="100%" border="0" cellspacing="0" cellpadding="4">
37     <tr>
38       <p>使用方法:直接点击“浏览...”上传zip文件,这里的文件只能是zip文件,zip文件和子文件以英文命名。</p>
39 <p>&nbsp;</p>
40  </td>
41     </tr>
42     <tr>
43       <td width="11%" nowrap>上传文件: </td>
44       <td width="89%"><input name="upfile" type="file" id="upfile" size="20"></td>
45     </tr>
46     <tr>
47 
48       <td><input name="todir" type="text" id="todir" style="display:none" value="pdf+images" size="15"></td>
49     </tr>
50 
51     <tr>
52       <td><input name="myaction" type="hidden" id="myaction" value="dounzip"></td>
53       <td><input type="submit" name="Submit" value=" 解 压 "></td>
54     </tr>
55   </table>
56 </form>
57 
58 
59  </body>
60 </html>
View Code

controllers/zip.php

php上传zip文件在线解压文件在指定目录下,CI框架版本php上传zip文件在线解压文件在指定目录下,CI框架版本
 1 <?php
 2 
 3 
 4 /*
 5  * Created on 2013-7-7
 6  *
 7  * To change the template for this generated file go to
 8  * Window - Preferences - PHPeclipse - PHP - Code Templates
 9  */
10 class Zip extends CI_Controller {
11     //构造函数
12     function __construct() {
13         parent :: __construct();
14         $this->load->helper('url');
15         $this->load->model("m_zip");
16         error_reporting(1);
17     }
18 
19     //显示zip文件录入信息界面
20     public function index() {
21         $this->load->view('admin/subject/information_zip');
22     }
23     //处理zip信息界面
24     public function zipUtil() {
25         if (!$_POST["todir"])
26             $_POST["todir"] = ".";
27         clearstatcache();
28 //        echo $_FILES["upfile"]["tmp_name"];
29         $this->start_unzip($_FILES["upfile"]["tmp_name"], $_FILES["upfile"]["name"], 1);
30     }
31     function start_unzip($tmp_name, $new_name, $checked) {
32         $have_zip_file = 0;
33         $upfile = array (
34             "tmp_name" => $tmp_name,
35             "name" => $new_name
36         );
37         if (is_file($upfile["tmp_name"])) {
38             $have_zip_file = 1;
39             $z = $this->m_zip;
40             echo "<br>正在解压: <input name='dfile[]' type='checkbox' value='$upfile[name]' " . ($checked ? "checked" : "") . "> $upfile[name]<br><br>";
41             if (preg_match('/\.zip$/mis', $upfile["name"])) {
42                 $result = $z->Extract($upfile["tmp_name"], $_POST["todir"]);
43                 if ($result == -1) {
44                     echo "<br>文件 $upfile[name] 错误.<br>";
45                 }
46                 echo "<br>完成,$z->total_files 个文件.<br><br><br>";
47 
48 
49             } else {
50                 echo "<br>$upfile[name] 不是 zip 文件.<br><br>";
51             }
52             if (realpath($upfile["name"]) != realpath($upfile["tmp_name"])) {
53                 @ unlink($upfile["name"]);
54                 rename($upfile["tmp_name"], $upfile["name"]);
55                 unlink($upfile["name"]);
56             }
57         }
58 
59     }
60 }
61 ?>
View Code

models/m_zip.php

php上传zip文件在线解压文件在指定目录下,CI框架版本php上传zip文件在线解压文件在指定目录下,CI框架版本
  1 <?php
  2 class m_zip extends CI_Model {
  3     var $total_files = 0;
  4     var $total_folders = 0;
  5 
  6     function Extract($zn, $to, $index = Array (
  7         -1
  8     )) {
  9         $ok = 0;
 10         $zip = @ fopen($zn, 'rb');
 11         if (!$zip)
 12             return (-1);
 13         $cdir = $this->ReadCentralDir($zip, $zn);
 14         $pos_entry = $cdir['offset'];
 15 
 16         if (!is_array($index)) {
 17             $index = array (
 18                 $index
 19             );
 20         }
 21         for ($i = 0; $index[$i]; $i++) {
 22             if (intval($index[$i]) != $index[$i] || $index[$i] > $cdir['entries'])
 23                 return (-1);
 24         }
 25         for ($i = 0; $i < $cdir['entries']; $i++) {
 26             @ fseek($zip, $pos_entry);
 27             $header = $this->ReadCentralFileHeaders($zip);
 28             $header['index'] = $i;
 29             $pos_entry = ftell($zip);
 30             @ rewind($zip);
 31             fseek($zip, $header['offset']);
 32             if (in_array("-1", $index) || in_array($i, $index))
 33                 $stat[$header['filename']] = $this->ExtractFile($header, $to, $zip);
 34         }
 35         fclose($zip);
 36         return $stat;
 37     }
 38 
 39     function ReadFileHeader($zip) {
 40         $binary_data = fread($zip, 30);
 41         $data = unpack('vchk/vid/vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $binary_data);
 42 
 43         $header['filename'] = fread($zip, $data['filename_len']);
 44         if ($data['extra_len'] != 0) {
 45             $header['extra'] = fread($zip, $data['extra_len']);
 46         } else {
 47             $header['extra'] = '';
 48         }
 49 
 50         $header['compression'] = $data['compression'];
 51         $header['size'] = $data['size'];
 52         $header['compressed_size'] = $data['compressed_size'];
 53         $header['crc'] = $data['crc'];
 54         $header['flag'] = $data['flag'];
 55         $header['mdate'] = $data['mdate'];
 56         $header['mtime'] = $data['mtime'];
 57 
 58         if ($header['mdate'] && $header['mtime']) {
 59             $hour = ($header['mtime'] & 0xF800) >> 11;
 60             $minute = ($header['mtime'] & 0x07E0) >> 5;
 61             $seconde = ($header['mtime'] & 0x001F) * 2;
 62             $year = (($header['mdate'] & 0xFE00) >> 9) + 1980;
 63             $month = ($header['mdate'] & 0x01E0) >> 5;
 64             $day = $header['mdate'] & 0x001F;
 65             $header['mtime'] = mktime($hour, $minute, $seconde, $month, $day, $year);
 66         } else {
 67             $header['mtime'] = time();
 68         }
 69 
 70         $header['stored_filename'] = $header['filename'];
 71         $header['status'] = "ok";
 72         return $header;
 73     }
 74 
 75     function ReadCentralFileHeaders($zip) {
 76         $binary_data = fread($zip, 46);
 77         $header = unpack('vchkid/vid/vversion/vversion_extracted/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/Voffset', $binary_data);
 78 
 79         if ($header['filename_len'] != 0)
 80             $header['filename'] = fread($zip, $header['filename_len']);
 81         else
 82             $header['filename'] = '';
 83 
 84         if ($header['extra_len'] != 0)
 85             $header['extra'] = fread($zip, $header['extra_len']);
 86         else
 87             $header['extra'] = '';
 88 
 89         if ($header['comment_len'] != 0)
 90             $header['comment'] = fread($zip, $header['comment_len']);
 91         else
 92             $header['comment'] = '';
 93 
 94         if ($header['mdate'] && $header['mtime']) {
 95             $hour = ($header['mtime'] & 0xF800) >> 11;
 96             $minute = ($header['mtime'] & 0x07E0) >> 5;
 97             $seconde = ($header['mtime'] & 0x001F) * 2;
 98             $year = (($header['mdate'] & 0xFE00) >> 9) + 1980;
 99             $month = ($header['mdate'] & 0x01E0) >> 5;
100             $day = $header['mdate'] & 0x001F;
101             $header['mtime'] = mktime($hour, $minute, $seconde, $month, $day, $year);
102         } else {
103             $header['mtime'] = time();
104         }
105         $header['stored_filename'] = $header['filename'];
106         $header['status'] = 'ok';
107         if (substr($header['filename'], -1) == '/')
108             $header['external'] = 0x41FF0010;
109         return $header;
110     }
111 
112     function ReadCentralDir($zip, $zip_name) {
113         $size = filesize($zip_name);
114 
115         if ($size < 277)
116             $maximum_size = $size;
117         else
118             $maximum_size = 277;
119 
120         @ fseek($zip, $size - $maximum_size);
121         $pos = ftell($zip);
122         $bytes = 0x00000000;
123 
124         while ($pos < $size) {
125             $byte = @ fread($zip, 1);
126             $bytes = ($bytes << 8) | ord($byte);
127             if ($bytes == 0x504b0506 or $bytes == 0x2e706870504b0506) {
128                 $pos++;
129                 break;
130             }
131             $pos++;
132         }
133 
134         $fdata = fread($zip, 18);
135 
136         $data = @ unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $fdata);
137 
138         if ($data['comment_size'] != 0)
139             $centd['comment'] = fread($zip, $data['comment_size']);
140         else
141             $centd['comment'] = '';
142         $centd['entries'] = $data['entries'];
143         $centd['disk_entries'] = $data['disk_entries'];
144         $centd['offset'] = $data['offset'];
145         $centd['disk_start'] = $data['disk_start'];
146         $centd['size'] = $data['size'];
147         $centd['disk'] = $data['disk'];
148         return $centd;
149     }
150 
151     function ExtractFile($header, $to, $zip) {
152         $header = $this->readfileheader($zip);
153 
154         if (substr($to, -1) != "/")
155             $to .= "/";
156         if ($to == './')
157             $to = '';
158         $pth = explode("/", $to . $header['filename']);
159         $mydir = '';
160         for ($i = 0; $i < count($pth) - 1; $i++) {
161             if (!$pth[$i])
162                 continue;
163             $mydir .= $pth[$i] . "/";
164             if ((!is_dir($mydir) && @ mkdir($mydir, 0777)) || (($mydir == $to . $header['filename'] || ($mydir == $to && $this->total_folders == 0)) && is_dir($mydir))) {
165                 @ chmod($mydir, 0777);
166                 $this->total_folders++;
167                 echo "<input name='dfile[]' type='checkbox' value='$mydir' checked> <a href='$mydir' target='_blank'>目录: $mydir</a><br>";
168             }
169         }
170 
171         if (strrchr($header['filename'], '/') == '/')
172             return;
173 
174         if (!($header['external'] == 0x41FF0010) && !($header['external'] == 16)) {
175             if ($header['compression'] == 0) {
176                 $fp = @ fopen($to . $header['filename'], 'wb');
177                 if (!$fp)
178                     return (-1);
179                 $size = $header['compressed_size'];
180 
181                 while ($size != 0) {
182                     $read_size = ($size < 2048 ? $size : 2048);
183                     $buffer = fread($zip, $read_size);
184                     $binary_data = pack('a' . $read_size, $buffer);
185                     @ fwrite($fp, $binary_data, $read_size);
186                     $size -= $read_size;
187                 }
188                 fclose($fp);
189                 touch($to . $header['filename'], $header['mtime']);
190             } else {
191                 $fp = @ fopen($to . $header['filename'] . '.gz', 'wb');
192                 if (!$fp)
193                     return (-1);
194                 $binary_data = pack('va1a1Va1a1', 0x8b1f, Chr($header['compression']), Chr(0x00), time(), Chr(0x00), Chr(3));
195 
196                 fwrite($fp, $binary_data, 10);
197                 $size = $header['compressed_size'];
198 
199                 while ($size != 0) {
200                     $read_size = ($size < 1024 ? $size : 1024);
201                     $buffer = fread($zip, $read_size);
202                     $binary_data = pack('a' . $read_size, $buffer);
203                     @ fwrite($fp, $binary_data, $read_size);
204                     $size -= $read_size;
205                 }
206 
207                 $binary_data = pack('VV', $header['crc'], $header['size']);
208                 fwrite($fp, $binary_data, 8);
209                 fclose($fp);
210 
211                 $gzp = @ gzopen($to . $header['filename'] . '.gz', 'rb') or die("Cette archive est compress閑");
212                 if (!$gzp)
213                     return (-2);
214                 $fp = @ fopen($to . $header['filename'], 'wb');
215                 if (!$fp)
216                     return (-1);
217                 $size = $header['size'];
218 
219                 while ($size != 0) {
220                     $read_size = ($size < 2048 ? $size : 2048);
221                     $buffer = gzread($gzp, $read_size);
222                     $binary_data = pack('a' . $read_size, $buffer);
223                     @ fwrite($fp, $binary_data, $read_size);
224                     $size -= $read_size;
225                 }
226                 fclose($fp);
227                 gzclose($gzp);
228 
229                 touch($to . $header['filename'], $header['mtime']);
230                 @ unlink($to . $header['filename'] . '.gz');
231 
232             }
233         }
234 
235         $this->total_files++;
236         echo "<input name='dfile[]' type='checkbox' value='$to$header[filename]' checked> <a href='$to$header[filename]' target='_blank'>文件: $to$header[filename]</a><br>";
237 
238         return true;
239     }
240 
241     // end class
242 }
243 ?>
View Code