Use PHPZip to decompress the file

Source: Internet
Author: User
Tags bit set crc32
Use PHPZip to decompress the file

  1. #
  2. # PHPZip v1.2 by Sext (sext@neud.net) 2002-11-18
  3. # (Changed: 2003-001)
  4. #
  5. # Makes zip archive
  6. #
  7. # Based on "Zip file creation class", uses zLib
  8. #
  9. #
  10. Class PHPZip
  11. {
  12. Function Zip ($ dir, $ zipfilename)
  13. {
  14. If (@ function_exists ('gzcompress '))
  15. {
  16. $ Curdir = getcwd ();
  17. If (is_array ($ dir ))
  18. {
  19. $ Filelist = $ dir;
  20. }
  21. Else
  22. {
  23. $ Filelist = $ this-> GetFileList ($ dir );
  24. }
  25. If ((! Empty ($ dir ))&&(! Is_array ($ dir) & (file_exists ($ dir) chdir ($ dir );
  26. Else chdir ($ curdir );
  27. If (count ($ filelist)> 0)
  28. {
  29. Foreach ($ filelist as $ filename)
  30. {
  31. If (is_file ($ filename ))
  32. {
  33. $ Fd = fopen ($ filename, "r ");
  34. $ Content = fread ($ fd, filesize ($ filename ));
  35. Fclose ($ fd );
  36. If (is_array ($ dir) $ filename = basename ($ filename );
  37. $ This-> addFile ($ content, $ filename );
  38. }
  39. }
  40. $ Out = $ this-> file ();
  41. Chdir ($ curdir );
  42. $ Fp = fopen ($ zipfilename, "w ");
  43. Fwrite ($ fp, $ out, strlen ($ out ));
  44. Fclose ($ fp );
  45. }
  46. Return 1;
  47. }
  48. Else return 0;
  49. }
  50. Function GetFileList ($ dir)
  51. {
  52. If (file_exists ($ dir ))
  53. {
  54. $ Args = func_get_args ();
  55. $ Pref = $ args [1];
  56. $ Dh = opendir ($ dir );
  57. While ($ files = readdir ($ dh ))
  58. {
  59. If ($ files! = ".") & ($ Files! = ".."))
  60. {
  61. If (is_dir ($ dir. $ files ))
  62. {
  63. $ Curdir = getcwd ();
  64. Chdir ($ dir. $ files );
  65. $ File = array_merge ($ file, $ this-> GetFileList ("", "$ pref $ files /"));
  66. Chdir ($ curdir );
  67. }
  68. Else $ file [] = $ pref. $ files;
  69. }
  70. }
  71. Closedir ($ dh );
  72. }
  73. Return $ file;
  74. }
  75. Var $ datasec = array ();
  76. Var $ ctrl_dir = array ();
  77. Var $ eof_ctrl_dir = "x50x4bx05x06x00x00x00x00 ";
  78. Var $ old_offset = 0;
  79. /**
  80. * Converts an Unix timestamp to a four byte DOS date and time format (date
  81. * In high two bytes, time in low two bytes allowing magn1_comparison ).
  82. *
  83. * @ Param integer the current Unix timestamp
  84. *
  85. * @ Return integer the current date in a four byte DOS format
  86. *
  87. * @ Access private
  88. */
  89. Function unix2DosTime ($ unixtime = 0 ){
  90. $ Timearray = ($ unixtime = 0 )? Getdate (): getdate ($ unixtime );
  91. If ($ timearray ['Year'] <1980 ){
  92. $ Timearray ['Year'] = 1980;
  93. $ Timearray ['mon'] = 1;
  94. $ Timearray ['mday'] = 1;
  95. $ Timearray ['urs'] = 0;
  96. $ Timearray ['minutes '] = 0;
  97. $ Timearray ['seconds'] = 0;
  98. } // End if
  99. Return ($ timearray ['Year']-1980) <25) | ($ timearray ['mon'] <21) | ($ timearray ['mday'] <16) |
  100. ($ Timearray ['urs'] <11) | ($ timearray ['minutes '] <5) | ($ timearray ['seconds']> 1 );
  101. } // End of the 'unix2dostime () 'method
  102. /**
  103. * Adds "file" to archive
  104. *
  105. * @ Param string file contents
  106. * @ Param string name of the file in the archive (may contains the path)
  107. * @ Param integer the current timestamp
  108. *
  109. * @ Access public
  110. */
  111. Function addFile ($ data, $ name, $ time = 0)
  112. {
  113. $ Name = str_replace ('', '/', $ name );
  114. $ Dtime = dechex ($ this-> unix2DosTime ($ time ));
  115. $ Hexdtime = 'x'. $ dtime [6]. $ dtime [7]
  116. . 'X'. $ dtime [4]. $ dtime [5]
  117. . 'X'. $ dtime [2]. $ dtime [3]
  118. . 'X'. $ dtime [0]. $ dtime [1];
  119. Eval ('$ hexdtime = "'. $ hexdtime .'";');
  120. $ Fr = "x50x4bx03x04 ";
  121. $ Fr. = "x14x00"; // ver needed to extract
  122. $ Fr. = "x00x00"; // gen purpose bit flag
  123. $ Fr. = "x08x00"; // compression method
  124. $ Fr. = $ hexdtime; // last mod time and date
  125. // "Local file header" segment
  126. $ Unc_len = strlen ($ data );
  127. $ Crc = crc32 ($ data );
  128. $ Zdata = gzcompress ($ data );
  129. $ C_len = strlen ($ zdata );
  130. $ Zdata = substr ($ zdata, 0, strlen ($ zdata)-4), 2); // fix the crc bug
  131. $ Fr. = pack ('v', $ crc); // crc32
  132. $ Fr. = pack ('v', $ c_len); // compressed filesize
  133. $ Fr. = pack ('v', $ unc_len); // uncompressed filesize
  134. $ Fr. = pack ('v', strlen ($ name); // length of filename
  135. $ Fr. = pack ('v', 0); // extra field length
  136. $ Fr. = $ name;
  137. // "File data" segment
  138. $ Fr. = $ zdata;
  139. // "Data descriptor" segment (optional but necessary if archive is not
  140. // Served as file)
  141. $ Fr. = pack ('v', $ crc); // crc32
  142. $ Fr. = pack ('v', $ c_len); // compressed filesize
  143. $ Fr. = pack ('v', $ unc_len); // uncompressed filesize
  144. // Add this entry to array
  145. $ This-> datasec [] = $ fr;
  146. $ New_offset = strlen (implode ('', $ this-> datasec ));
  147. // Now add to central directory record
  148. $ Cdrec = "x50x4bx01x02 ";
  149. $ Cdrec. = "x00x00"; // version made
  150. $ Cdrec. = "x14x00"; // version needed to extract
  151. $ Cdrec. = "x00x00"; // gen purpose bit flag
  152. $ Cdrec. = "x08x00"; // compression method
  153. $ Cdrec. = $ hexdtime; // last mod time & date
  154. $ Cdrec. = pack ('v', $ crc); // crc32
  155. $ Cdrec. = pack ('v', $ c_len); // compressed filesize
  156. $ Cdrec. = pack ('v', $ unc_len); // uncompressed filesize
  157. $ Cdrec. = pack ('v', strlen ($ name); // length of filename
  158. $ Cdrec. = pack ('v', 0); // extra field length
  159. $ Cdrec. = pack ('v', 0); // file comment length
  160. $ Cdrec. = pack ('v', 0); // disk number start
  161. $ Cdrec. = pack ('v', 0); // internal file attributes
  162. $ Cdrec. = pack ('v', 32); // external file attributes-'archive' bit set
  163. $ Cdrec. = pack ('v', $ this-> old_offset); // relative offset of local header
  164. $ This-> old_offset = $ new_offset;
  165. $ Cdrec. = $ name;
  166. // Optional extra field, file comment goes here
  167. // Save to central directory
  168. $ This-> ctrl_dir [] = $ cdrec;
  169. } // End of the 'addfile () 'method
  170. /**
  171. * Dumps out file
  172. *
  173. * @ Return string the zipped file
  174. *
  175. * @ Access public
  176. */
  177. Function file ()
  178. {
  179. $ Data = implode ('', $ this-> datasec );
  180. $ Ctrldir = implode ('', $ this-> ctrl_dir );
  181. Return
  182. $ Data.
  183. $ Ctrldir.
  184. $ This-> eof_ctrl_dir.
  185. Pack ('v', sizeof ($ this-> ctrl_dir). // total # of entries "on this disk"
  186. Pack ('v', sizeof ($ this-> ctrl_dir). // total # of entries overall
  187. Pack ('v', strlen ($ ctrldir). // size of central dir
  188. Pack ('v', strlen ($ data). // offset to start of central dir
  189. "X00x00"; //. zip file comment length
  190. } // End of the 'file () 'method
  191. } // End of the 'phpzip' class
  192. ?>
  193. // Usage
  194. $ Z = new PHPZip (); // Create a zip class
  195. Method 1:
  196. $ Z-> Zip ("", "out1.zip"); // add all files under the current directory and subdirectory
  197. Method 2:
  198. Using filespolicarray('1.txt', 'gb.txt ');
  199. Using files000000000000'5.txt ';
  200. $ Z-> Zip ($ files, "out2.zip"); // add a file list
  201. Method 3:
  202. $ Z-> Zip ("/usr/local/sext/", "out3.zip"); // add a specified directory
  203. ?>


Decompress, PHPZip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.