class_zip.php 8.0 KB

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