class_zip.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: class_zip.php 27449 2012-02-01 05:32:35Z zhangguosheng $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class zipfile {
  12. var $datasec = array();
  13. var $ctrl_dir = array();
  14. var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
  15. var $old_offset = 0;
  16. function unix2DosTime($unixtime = 0) {
  17. $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
  18. if($timearray['year'] < 1980) {
  19. $timearray['year'] = 1980;
  20. $timearray['mon'] = 1;
  21. $timearray['mday'] = 1;
  22. $timearray['hours'] = 0;
  23. $timearray['minutes'] = 0;
  24. $timearray['seconds'] = 0;
  25. } // end if
  26. return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) |
  27. ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);
  28. } // end of the 'unix2DosTime()' method
  29. function addFile($data, $name, $time = 0) {
  30. $name = str_replace('\\', '/', $name);
  31. $dtime = dechex($this->unix2DosTime($time));
  32. $hexdtime = '\x' . $dtime[6] . $dtime[7]
  33. . '\x' . $dtime[4] . $dtime[5]
  34. . '\x' . $dtime[2] . $dtime[3]
  35. . '\x' . $dtime[0] . $dtime[1];
  36. eval('$hexdtime = "' . $hexdtime . '";');
  37. $fr = "\x50\x4b\x03\x04";
  38. $fr .= "\x14\x00"; // ver needed to extract
  39. $fr .= "\x00\x00"; // gen purpose bit flag
  40. $fr .= "\x08\x00"; // compression method
  41. $fr .= $hexdtime; // last mod time and date
  42. $unc_len = strlen($data);
  43. $crc = crc32($data);
  44. $zdata = gzcompress($data);
  45. $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
  46. $c_len = strlen($zdata);
  47. $fr .= pack('V', $crc); // crc32
  48. $fr .= pack('V', $c_len); // compressed filesize
  49. $fr .= pack('V', $unc_len); // uncompressed filesize
  50. $fr .= pack('v', strlen($name)); // length of filename
  51. $fr .= pack('v', 0); // extra field length
  52. $fr .= $name;
  53. $fr .= $zdata;
  54. $this -> datasec[] = $fr;
  55. $cdrec = "\x50\x4b\x01\x02";
  56. $cdrec .= "\x00\x00"; // version made by
  57. $cdrec .= "\x14\x00"; // version needed to extract
  58. $cdrec .= "\x00\x00"; // gen purpose bit flag
  59. $cdrec .= "\x08\x00"; // compression method
  60. $cdrec .= $hexdtime; // last mod time & date
  61. $cdrec .= pack('V', $crc); // crc32
  62. $cdrec .= pack('V', $c_len); // compressed filesize
  63. $cdrec .= pack('V', $unc_len); // uncompressed filesize
  64. $cdrec .= pack('v', strlen($name) ); // length of filename
  65. $cdrec .= pack('v', 0 ); // extra field length
  66. $cdrec .= pack('v', 0 ); // file comment length
  67. $cdrec .= pack('v', 0 ); // disk number start
  68. $cdrec .= pack('v', 0 ); // internal file attributes
  69. $cdrec .= pack('V', 32 ); // external file attributes - 'archive' bit set
  70. $cdrec .= pack('V', $this -> old_offset ); // relative offset of local header
  71. $this -> old_offset += strlen($fr);
  72. $cdrec .= $name;
  73. $this -> ctrl_dir[] = $cdrec;
  74. } // end of the 'addFile()' method
  75. function file() {
  76. $data = implode('', $this -> datasec);
  77. $ctrldir = implode('', $this -> ctrl_dir);
  78. return
  79. $data .
  80. $ctrldir .
  81. $this -> eof_ctrl_dir .
  82. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries "on this disk"
  83. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries overall
  84. pack('V', strlen($ctrldir)) . // size of central dir
  85. pack('V', strlen($data)) . // offset to start of central dir
  86. "\x00\x00"; // .zip file comment length
  87. } // end of the 'file()' method
  88. } // end of the 'zipfile' class
  89. class SimpleUnzip {
  90. var $Comment = '';
  91. var $Entries = array();
  92. var $Name = '';
  93. var $Size = 0;
  94. var $Time = 0;
  95. function SimpleUnzip($in_FileName = '') {
  96. if($in_FileName !== '') {
  97. SimpleUnzip::ReadFile($in_FileName);
  98. }
  99. } // end of the 'SimpleUnzip' constructor
  100. function Count() {
  101. return count($this->Entries);
  102. } // end of the 'Count()' method
  103. function GetData($in_Index) {
  104. return $this->Entries[$in_Index]->Data;
  105. } // end of the 'GetData()' method
  106. function GetEntry($in_Index) {
  107. return $this->Entries[$in_Index];
  108. } // end of the 'GetEntry()' method
  109. function GetError($in_Index) {
  110. return $this->Entries[$in_Index]->Error;
  111. } // end of the 'GetError()' method
  112. function GetErrorMsg($in_Index) {
  113. return $this->Entries[$in_Index]->ErrorMsg;
  114. } // end of the 'GetErrorMsg()' method
  115. function GetName($in_Index) {
  116. return $this->Entries[$in_Index]->Name;
  117. } // end of the 'GetName()' method
  118. function GetPath($in_Index) {
  119. return $this->Entries[$in_Index]->Path;
  120. } // end of the 'GetPath()' method
  121. function GetTime($in_Index) {
  122. return $this->Entries[$in_Index]->Time;
  123. } // end of the 'GetTime()' method
  124. function ReadFile($in_FileName) {
  125. $this->Entries = array();
  126. $this->Name = $in_FileName;
  127. $this->Time = filemtime($in_FileName);
  128. $this->Size = filesize($in_FileName);
  129. $oF = fopen($in_FileName, 'rb');
  130. $vZ = fread($oF, $this->Size);
  131. fclose($oF);
  132. $aE = explode("\x50\x4b\x05\x06", $vZ);
  133. $aP = unpack('x16/v1CL', $aE[1]);
  134. $this->Comment = substr($aE[1], 18, $aP['CL']);
  135. $this->Comment = strtr($this->Comment, array("\r\n" => "\n",
  136. "\r" => "\n"));
  137. $aE = explode("\x50\x4b\x01\x02", $vZ);
  138. $aE = explode("\x50\x4b\x03\x04", $aE[0]);
  139. array_shift($aE);
  140. foreach($aE as $vZ) {
  141. $aI = array();
  142. $aI['E'] = 0;
  143. $aI['EM'] = '';
  144. $aP = unpack('v1VN/v1GPF/v1CM/v1FT/v1FD/V1CRC/V1CS/V1UCS/v1FNL', $vZ);
  145. $bE = ($aP['GPF'] && 0x0001) ? TRUE : FALSE;
  146. $nF = $aP['FNL'];
  147. if($aP['GPF'] & 0x0008) {
  148. $aP1 = unpack('V1CRC/V1CS/V1UCS', substr($vZ, -12));
  149. $aP['CRC'] = $aP1['CRC'];
  150. $aP['CS'] = $aP1['CS'];
  151. $aP['UCS'] = $aP1['UCS'];
  152. $vZ = substr($vZ, 0, -12);
  153. }
  154. $aI['N'] = substr($vZ, 26, $nF);
  155. if(substr($aI['N'], -1) == '/') {
  156. continue;
  157. }
  158. $aI['P'] = dirname($aI['N']);
  159. $aI['P'] = $aI['P'] == '.' ? '' : $aI['P'];
  160. $aI['N'] = basename($aI['N']);
  161. $vZ = substr($vZ, 26 + $nF);
  162. if(strlen($vZ) != $aP['CS']) {
  163. $aI['E'] = 1;
  164. $aI['EM'] = 'Compressed size is not equal with the value in header information.';
  165. } else {
  166. if($bE) {
  167. $aI['E'] = 5;
  168. $aI['EM'] = 'File is encrypted, which is not supported from this class.';
  169. } else {
  170. switch($aP['CM']) {
  171. case 0: // Stored
  172. break;
  173. case 8: // Deflated
  174. $vZ = gzinflate($vZ);
  175. break;
  176. case 12: // BZIP2
  177. if(! extension_loaded('bz2')) {
  178. if(strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
  179. @dl('php_bz2.dll');
  180. } else {
  181. @dl('bz2.so');
  182. }
  183. }
  184. if(extension_loaded('bz2')) {
  185. $vZ = bzdecompress($vZ);
  186. } else {
  187. $aI['E'] = 7;
  188. $aI['EM'] = "PHP BZIP2 extension not available.";
  189. }
  190. break;
  191. default:
  192. $aI['E'] = 6;
  193. $aI['EM'] = "De-/Compression method {$aP['CM']} is not supported.";
  194. }
  195. if(! $aI['E']) {
  196. if($vZ === FALSE) {
  197. $aI['E'] = 2;
  198. $aI['EM'] = 'Decompression of data failed.';
  199. } else {
  200. if(strlen($vZ) != $aP['UCS']) {
  201. $aI['E'] = 3;
  202. $aI['EM'] = 'Uncompressed size is not equal with the value in header information.';
  203. } else {
  204. if(crc32($vZ) != $aP['CRC']) {
  205. $aI['E'] = 4;
  206. $aI['EM'] = 'CRC32 checksum is not equal with the value in header information.';
  207. }
  208. }
  209. }
  210. }
  211. }
  212. }
  213. $aI['D'] = $vZ;
  214. $aI['T'] = mktime(($aP['FT'] & 0xf800) >> 11,
  215. ($aP['FT'] & 0x07e0) >> 5,
  216. ($aP['FT'] & 0x001f) << 1,
  217. ($aP['FD'] & 0x01e0) >> 5,
  218. ($aP['FD'] & 0x001f),
  219. (($aP['FD'] & 0xfe00) >> 9) + 1980);
  220. $this->Entries[] = new SimpleUnzipEntry($aI);
  221. } // end for each entries
  222. return $this->Entries;
  223. } // end of the 'ReadFile()' method
  224. } // end of the 'SimpleUnzip' class
  225. class SimpleUnzipEntry {
  226. var $Data = '';
  227. var $Error = 0;
  228. var $ErrorMsg = '';
  229. var $Name = '';
  230. var $Path = '';
  231. var $Time = 0;
  232. function SimpleUnzipEntry($in_Entry) {
  233. $this->Data = $in_Entry['D'];
  234. $this->Error = $in_Entry['E'];
  235. $this->ErrorMsg = $in_Entry['EM'];
  236. $this->Name = $in_Entry['N'];
  237. $this->Path = $in_Entry['P'];
  238. $this->Time = $in_Entry['T'];
  239. } // end of the 'SimpleUnzipEntry' constructor
  240. } // end of the 'SimpleUnzipEntry' class
  241. ?>