Phpzip.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <?php
  2. class Phpzip extends Think {
  3. private $ctrl_dir = array();
  4. private $datasec = array();
  5. /**********************************************************
  6. * 压缩部分
  7. * **********************************************************/
  8. // ------------------------------------------------------ //
  9. // #遍历指定文件夹
  10. //
  11. // $archive = new PHPZip();
  12. // $filelist = $archive->visitFile(文件夹路径);
  13. // print "当前文件夹的文件:<p>\r\n";
  14. // foreach($filelist as $file)
  15. // printf("%s<br>\r\n", $file);
  16. // ------------------------------------------------------ //
  17. var $fileList = array();
  18. public function visitFile($path)
  19. {
  20. global $fileList;
  21. $path = str_replace("\\", "/", $path);
  22. $fdir = dir($path);
  23. while(($file = $fdir->read()) !== false)
  24. {
  25. if($file == '.' || $file == '..'){ continue; }
  26. $pathSub = preg_replace("*/{2,}*", "/", $path."/".$file); // 替换多个反斜杠
  27. $fileList[] = is_dir($pathSub) ? $pathSub."/" : $pathSub;
  28. if(is_dir($pathSub)){ self::visitFile($pathSub); } }
  29. $fdir->close();
  30. return $fileList;
  31. }
  32. private function unix2DosTime($unixtime = 0)
  33. {
  34. $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
  35. if($timearray['year'] < 1980)
  36. {
  37. $timearray['year'] = 1980;
  38. $timearray['mon'] = 1;
  39. $timearray['mday'] = 1;
  40. $timearray['hours'] = 0;
  41. $timearray['minutes'] = 0;
  42. $timearray['seconds'] = 0;
  43. }
  44. return (
  45. ($timearray['year'] - 1980) << 25)
  46. | ($timearray['mon'] << 21)
  47. | ($timearray['mday'] << 16)
  48. | ($timearray['hours'] << 11)
  49. | ($timearray['minutes'] << 5)
  50. | ($timearray['seconds'] >> 1);
  51. }
  52. var $old_offset = 0;
  53. private function addFile($data, $filename, $time = 0)
  54. {
  55. $filename = str_replace('\\', '/', $filename);
  56. $dtime = dechex(self::unix2DosTime($time));
  57. $hexdtime = '\x' . $dtime[6] . $dtime[7]
  58. . '\x' . $dtime[4] . $dtime[5]. '\x'
  59. . $dtime[2] . $dtime[3]. '\x'
  60. . $dtime[0] . $dtime[1];
  61. eval('$hexdtime = "' . $hexdtime . '";');
  62. $fr = "\x50\x4b\x03\x04";
  63. $fr .= "\x14\x00";
  64. $fr .= "\x00\x00";
  65. $fr .= "\x08\x00";
  66. $fr .= $hexdtime;
  67. $unc_len = strlen($data);
  68. $crc = crc32($data);
  69. $zdata = gzcompress($data);
  70. $c_len = strlen($zdata);
  71. $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2);
  72. $fr .= pack('V', $crc);
  73. $fr .= pack('V', $c_len);
  74. $fr .= pack('V', $unc_len);
  75. $fr .= pack('v', strlen($filename));
  76. $fr .= pack('v', 0);
  77. $fr .= $filename;
  78. $fr .= $zdata;
  79. $fr .= pack('V', $crc);
  80. $fr .= pack('V', $c_len);
  81. $fr .= pack('V', $unc_len);
  82. $this->datasec[] = $fr;
  83. $new_offset = strlen(implode('', $this->datasec));
  84. $cdrec = "\x50\x4b\x01\x02";
  85. $cdrec .= "\x00\x00";
  86. $cdrec .= "\x14\x00";
  87. $cdrec .= "\x00\x00";
  88. $cdrec .= "\x08\x00";
  89. $cdrec .= $hexdtime;
  90. $cdrec .= pack('V', $crc);
  91. $cdrec .= pack('V', $c_len);
  92. $cdrec .= pack('V', $unc_len);
  93. $cdrec .= pack('v', strlen($filename) );
  94. $cdrec .= pack('v', 0 );
  95. $cdrec .= pack('v', 0 );
  96. $cdrec .= pack('v', 0 );
  97. $cdrec .= pack('v', 0 );
  98. $cdrec .= pack('V', 32 );
  99. $cdrec .= pack('V', $this->old_offset );
  100. $this->old_offset = $new_offset;
  101. $cdrec .= $filename;
  102. $this->ctrl_dir[] = $cdrec;
  103. }
  104. var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
  105. private function file(){
  106. $data = implode('', $this->datasec);
  107. $ctrldir = implode('', $this->ctrl_dir);
  108. return $data . $ctrldir. $this->eof_ctrl_dir
  109. .pack('v', sizeof($this->ctrl_dir))
  110. .pack('v', sizeof($this->ctrl_dir))
  111. .pack('V', strlen($ctrldir))
  112. .pack('V', strlen($data)). "\x00\x00";
  113. }
  114. // ------------------------------------------------------ //
  115. // #压缩到服务器 //
  116. // $archive = new PHPZip();
  117. // $archive->Zip("需压缩的文件所在目录", "ZIP压缩文件名");
  118. // ------------------------------------------------------ //
  119. public function Zip($dir, $saveName)
  120. {
  121. if(@!function_exists('gzcompress'))
  122. {
  123. ECHO 'fuck';exit;
  124. return;
  125. }
  126. $filelist = self::visitFile($dir);
  127. if(count($filelist) == 0)
  128. { return; }
  129. foreach($filelist as $file)
  130. {
  131. if(!file_exists($file) || !is_file($file)){ continue; }
  132. $fd = fopen($file, "rb");
  133. $content = @fread($fd, filesize($file));
  134. fclose($fd);
  135. // 1.删除$dir的字符(./folder/file.txt删除./folder/)
  136. // 2.如果存在/就删除(/file.txt删除/)
  137. $file = substr($file, strlen($dir));
  138. if(substr($file, 0, 1) == "\\" || substr($file, 0, 1) == "/"){ $file = substr($file, 1); }
  139. self::addFile($content, $file);
  140. }
  141. $out = self::file();
  142. $fp = fopen($saveName, "wb");
  143. fwrite($fp, $out, strlen($out));
  144. fclose($fp);
  145. }
  146. // ------------------------------------------------------ //
  147. // #压缩并直接下载 //
  148. // $archive = new PHPZip();
  149. // $archive->ZipAndDownload("需压缩的文件所在目录");
  150. // ------------------------------------------------------ //
  151. public function ZipAndDownload($dir)
  152. {
  153. if(@!function_exists('gzcompress'))
  154. {
  155. return;
  156. }
  157. $filelist = self::visitFile($dir);
  158. if(count($filelist) == 0)
  159. { return; }
  160. foreach($filelist as $file)
  161. {
  162. if(!file_exists($file) || !is_file($file)){ continue; }
  163. $fd = fopen($file, "rb");
  164. $content = @fread($fd, filesize($file));
  165. fclose($fd);
  166. // 1.删除$dir的字符(./folder/file.txt删除./folder/)
  167. // 2.如果存在/就删除(/file.txt删除/)
  168. $file = substr($file, strlen($dir));
  169. if(substr($file, 0, 1) == "\\" || substr($file, 0, 1) == "/")
  170. {
  171. $file = substr($file, 1); }
  172. self::addFile($content, $file);
  173. }
  174. $out = self::file();
  175. @header('Content-Encoding: none');
  176. @header('Content-Type: application/zip');
  177. @header('Content-Disposition: attachment ; filename=Farticle'.date("YmdHis", time()).'.zip');
  178. @header('Pragma: no-cache');
  179. @header('Expires: 0');
  180. print($out);
  181. }
  182. /*********************************************************** 解压部分 **********************************************************/
  183. // ------------------------------------------------------ //
  184. // ReadCentralDir($zip, $zipfile)
  185. // $zip是经过@fopen($zipfile, 'rb')打开的
  186. // $zipfile是zip文件的路径
  187. // ------------------------------------------------------ //
  188. private function ReadCentralDir($zip, $zipfile)
  189. {
  190. $size = filesize($zipfile);
  191. $max_size = ($size < 277) ? $size : 277;
  192. @fseek($zip, $size - $max_size);
  193. $pos = ftell($zip);
  194. $bytes = 0x00000000;
  195. while($pos < $size){
  196. $byte = @fread($zip, 1);
  197. $bytes = ($bytes << 8) | Ord($byte);
  198. $pos++;
  199. if($bytes == 0x504b0506){ break; }
  200. }
  201. $data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size',
  202. fread($zip, 18));
  203. $centd['comment'] = ($data['comment_size'] != 0) ? fread($zip, $data['comment_size']) : ''; // 注释
  204. $centd['entries'] = $data['entries'];
  205. $centd['disk_entries'] = $data['disk_entries'];
  206. $centd['offset'] = $data['offset'];
  207. $centd['disk_start'] = $data['disk_start'];
  208. $centd['size'] = $data['size'];
  209. $centd['disk'] = $data['disk'];
  210. return $centd;
  211. }
  212. private function ReadCentralFileHeaders($zip)
  213. {
  214. $binary_data = fread($zip, 46);
  215. $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);
  216. $header['filename'] = ($header['filename_len'] != 0) ? fread($zip, $header['filename_len']) : '';
  217. $header['extra'] = ($header['extra_len'] != 0) ? fread($zip, $header['extra_len']) : '';
  218. $header['comment'] = ($header['comment_len'] != 0) ? fread($zip, $header['comment_len']) : '';
  219. if($header['mdate'] && $header['mtime'])
  220. {
  221. $hour = ($header['mtime'] & 0xF800) >> 11;
  222. $minute = ($header['mtime'] & 0x07E0) >> 5;
  223. $seconde = ($header['mtime'] & 0x001F) * 2;
  224. $year = (($header['mdate'] & 0xFE00) >> 9) + 1980;
  225. $month = ($header['mdate'] & 0x01E0) >> 5;
  226. $day = $header['mdate'] & 0x001F;
  227. $header['mtime'] = mktime($hour, $minute, $seconde, $month, $day, $year);
  228. } else {
  229. $header['mtime'] = time(); }
  230. $header['stored_filename'] = $header['filename'];
  231. $header['status'] = 'ok';
  232. if(substr($header['filename'], -1) == '/'){
  233. $header['external'] = 0x41FF0010;
  234. } // 判断是否文件夹
  235. return $header;
  236. }
  237. private function ReadFileHeader($zip)
  238. {
  239. $binary_data = fread($zip, 30);
  240. $data = unpack('vchk/vid/vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $binary_data);
  241. $header['filename'] = fread($zip, $data['filename_len']);
  242. $header['extra'] = ($data['extra_len'] != 0) ? fread($zip, $data['extra_len']) : '';
  243. $header['compression'] = $data['compression'];
  244. $header['size'] = $data['size'];
  245. $header['compressed_size'] = $data['compressed_size'];
  246. $header['crc'] = $data['crc'];
  247. $header['flag'] = $data['flag'];
  248. $header['mdate'] = $data['mdate'];
  249. $header['mtime'] = $data['mtime'];
  250. if($header['mdate'] && $header['mtime']){
  251. $hour = ($header['mtime'] & 0xF800) >> 11;
  252. $minute = ($header['mtime'] & 0x07E0) >> 5;
  253. $seconde = ($header['mtime'] & 0x001F) * 2;
  254. $year = (($header['mdate'] & 0xFE00) >> 9) + 1980;
  255. $month = ($header['mdate'] & 0x01E0) >> 5;
  256. $day = $header['mdate'] & 0x001F;
  257. $header['mtime'] = mktime($hour, $minute, $seconde, $month, $day, $year);
  258. }else{
  259. $header['mtime'] = time();
  260. }
  261. $header['stored_filename'] = $header['filename'];
  262. $header['status'] = "ok";
  263. return $header;
  264. }
  265. private function ExtractFile($header, $to, $zip)
  266. {
  267. $header = self::readfileheader($zip);
  268. if(substr($to, -1) != "/"){ $to .= "/"; }
  269. if(!@is_dir($to)){ @mkdir($to, 0777); }
  270. $pth = explode("/", dirname($header['filename']));
  271. for($i=0; isset($pth[$i]); $i++){
  272. if(!$pth[$i]){ continue; }
  273. $pthss .= $pth[$i]."/";
  274. if(!is_dir($to.$pthss)){ @mkdir($to.$pthss, 0777); }
  275. }
  276. if(!($header['external'] == 0x41FF0010) && !($header['external'] == 16))
  277. {
  278. if($header['compression'] == 0)
  279. {
  280. $fp = @fopen($to.$header['filename'], 'wb');
  281. if(!$fp){ return(-1); }
  282. $size = $header['compressed_size'];
  283. while($size != 0)
  284. {
  285. $read_size = ($size < 2048 ? $size : 2048);
  286. $buffer = fread($zip, $read_size);
  287. $binary_data = pack('a'.$read_size, $buffer);
  288. @fwrite($fp, $binary_data, $read_size);
  289. $size -= $read_size;
  290. }
  291. fclose($fp);
  292. touch($to.$header['filename'], $header['mtime']);
  293. }else{
  294. $fp = @fopen($to.$header['filename'].'.gz', 'wb');
  295. if(!$fp)
  296. {
  297. return(-1);
  298. }
  299. $binary_data = pack('va1a1Va1a1', 0x8b1f, Chr($header['compression']), Chr(0x00), time(), Chr(0x00), Chr(3));
  300. fwrite($fp, $binary_data, 10);
  301. $size = $header['compressed_size'];
  302. while($size != 0)
  303. {
  304. $read_size = ($size < 1024 ? $size : 1024);
  305. $buffer = fread($zip, $read_size);
  306. $binary_data = pack('a'.$read_size, $buffer);
  307. @fwrite($fp, $binary_data, $read_size);
  308. $size -= $read_size;
  309. }
  310. $binary_data = pack('VV', $header['crc'], $header['size']);
  311. fwrite($fp, $binary_data, 8);
  312. fclose($fp);
  313. $gzp = @gzopen($to.$header['filename'].'.gz', 'rb') or die("Cette archive est compress!");
  314. if(!$gzp){ return(-2); }
  315. $fp = @fopen($to.$header['filename'], 'wb');
  316. if(!$fp)
  317. {
  318. return(-1);
  319. }
  320. $size = $header['size'];
  321. while($size != 0)
  322. {
  323. $read_size = ($size < 2048 ? $size : 2048);
  324. $buffer = gzread($gzp, $read_size);
  325. $binary_data = pack('a'.$read_size, $buffer);
  326. @fwrite($fp, $binary_data, $read_size);
  327. $size -= $read_size;
  328. }
  329. fclose($fp); gzclose($gzp);
  330. touch($to.$header['filename'], $header['mtime']);
  331. @unlink($to.$header['filename'].'.gz');
  332. }
  333. }
  334. return true;
  335. }
  336. // ------------------------------------------------------ //
  337. // #解压文件 //
  338. // $archive = new PHPZip();
  339. // $zipfile = "ZIP压缩文件名";
  340. // $savepath = "解压缩目录名";
  341. // $zipfile = $unzipfile;
  342. // $savepath = $unziptarget;
  343. // $array = $archive->GetZipInnerFilesInfo($zipfile);
  344. // $filecount = 0;
  345. // $dircount = 0;
  346. // $failfiles = array();
  347. // set_time_limit(0);
  348. // 修改为不限制超时时间(默认为30秒)
  349. //
  350. // for($i=0; $i<count($array); $i++) {
  351. // if($array[$i][folder] == 0){
  352. // if($archive->unZip($zipfile, $savepath, $i) > 0){
  353. // $filecount++;
  354. // }else{
  355. // $failfiles[] = $array[$i][filename];
  356. // }
  357. // }else{
  358. // $dircount++;
  359. // }
  360. // }
  361. // set_time_limit(30);
  362. //printf("文件夹:%d&nbsp;&nbsp;&nbsp;&nbsp;解压文件:%d&nbsp;&nbsp;&nbsp;&nbsp;失败:%d<br>\r\n", $dircount, $filecount, count($failfiles));
  363. //if(count($failfiles) > 0){
  364. // foreach($failfiles as $file){
  365. // printf("&middot;%s<br>\r\n", $file);
  366. // }
  367. //}
  368. // ------------------------------------------------------ //
  369. public function unZip($zipfile, $to, $index = Array(-1))
  370. {
  371. $ok = 0;
  372. $zip = @fopen($zipfile, 'rb');
  373. if(!$zip){ return(-1); }
  374. $cdir = self::ReadCentralDir($zip, $zipfile);
  375. $pos_entry = $cdir['offset'];
  376. if(!is_array($index)){ $index = array($index); }
  377. for($i=0; $index[$i]; $i++)
  378. {
  379. if(intval($index[$i]) != $index[$i] || $index[$i] > $cdir['entries'])
  380. {
  381. return(-1);
  382. }
  383. }
  384. for($i=0; $i<$cdir['entries']; $i++)
  385. {
  386. @fseek($zip, $pos_entry);
  387. $header = self::ReadCentralFileHeaders($zip);
  388. $header['index'] = $i;
  389. $pos_entry = ftell($zip);
  390. @rewind($zip);
  391. fseek($zip, $header['offset']);
  392. if(in_array("-1", $index) || in_array($i, $index))
  393. {
  394. $stat[$header['filename']] = self::ExtractFile($header, $to, $zip);
  395. }
  396. }
  397. fclose($zip);
  398. return $stat;
  399. }
  400. /********************************************************** * 其它部分 **********************************************************/
  401. // ------------------------------------------------------ //
  402. // #获取被压缩文件的信息 //
  403. // $archive = new PHPZip();
  404. // $array = $archive->GetZipInnerFilesInfo(ZIP压缩文件名);
  405. // for($i=0; $i<count($array); $i++) {
  406. // printf("<b>&middot;%s</b><br>\r\n", $array[$i][filename]);
  407. // foreach($array[$i] as $key => $value)
  408. // printf("%s => %s<br>\r\n", $key, $value);
  409. // print "\r\n<p>------------------------------------<p>\r\n\r\n";
  410. // }
  411. // ------------------------------------------------------ //
  412. public function GetZipInnerFilesInfo($zipfile)
  413. {
  414. $zip = @fopen($zipfile, 'rb');
  415. if(!$zip){ return(0); }
  416. $centd = self::ReadCentralDir($zip, $zipfile);
  417. @rewind($zip);
  418. @fseek($zip, $centd['offset']);
  419. $ret = array();
  420. for($i=0; $i<$centd['entries']; $i++)
  421. {
  422. $header = self::ReadCentralFileHeaders($zip);
  423. $header['index'] = $i;
  424. $info = array(
  425. 'filename' => $header['filename'], // 文件名
  426. 'stored_filename' => $header['stored_filename'], // 压缩后文件名
  427. 'size' => $header['size'], // 大小
  428. 'compressed_size' => $header['compressed_size'], // 压缩后大小
  429. 'crc' => strtoupper(dechex($header['crc'])), // CRC32
  430. 'mtime' => date("Y-m-d H:i:s",$header['mtime']), // 文件修改时间
  431. 'comment' => $header['comment'], // 注释
  432. 'folder' => ($header['external'] == 0x41FF0010 || $header['external'] == 16) ? 1 : 0, // 是否为文件夹
  433. 'index' => $header['index'], // 文件索引
  434. 'status' => $header['status'] // 状态
  435. );
  436. $ret[] = $info;
  437. unset($header);
  438. }
  439. fclose($zip);
  440. return $ret;
  441. }
  442. // ------------------------------------------------------ //
  443. // #获取压缩文件的注释 //
  444. // $archive = new PHPZip();
  445. // echo $archive->GetZipComment(ZIP压缩文件名);
  446. // ------------------------------------------------------ //
  447. public function GetZipComment($zipfile)
  448. {
  449. $zip = @fopen($zipfile, 'rb');
  450. if(!$zip){ return(0); }
  451. $centd = self::ReadCentralDir($zip, $zipfile);
  452. fclose($zip);
  453. return $centd[comment];
  454. }
  455. }
  456. ?>