PHPZip usage-php Tutorial

Source: Internet
Author: User
Tags bit set
Use of PHPZip

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


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.