A php package a group of files into a zip class.

Source: Internet
Author: User
Tags bit set crc32
A php package a group of files into a zip class.

This php class can add files to the array one by one, and finally package the added files into zip files.

  1. /* $ Id: zip. lib. php, v 1.1 2004/02/14 15:21:18 anoncvs_tusedb Exp $ */
  2. // Vim: expandtab sw = 4 ts = 4 sts = 4:
  3. /**
  4. * Zip file creation class.
  5. * Makes zip files.
  6. *
  7. * Last Modification and Extension:
  8. *
  9. * Hasin Hayder
  10. * HomePage: www.hasinme.info
  11. * Email: countdraculla@gmail.com
  12. * IDE: PHP Designer 2005
  13. *
  14. *
  15. * Originally Based on:
  16. *
  17. * Http://www.zend.com/codex.php? Id = 535 & single = 1
  18. * By Eric Mueller
  19. *
  20. * Http://www.zend.com/codex.php? Id = 470 & single = 1
  21. * By Denis125
  22. *
  23. * A patch from Peter Listiak For last modified
  24. * Date and time of the compressed file
  25. *
  26. * Official ZIP file format: http://www.pkware.com/appnote.txt
  27. *
  28. * @ Access public
  29. */
  30. Class zipfile
  31. {
  32. /**
  33. * Array to store compressed data
  34. *
  35. * @ Public array $ datasec
  36. */
  37. Public $ datasec = array ();
  38. /**
  39. * Central directory
  40. *
  41. * @ Public array $ ctrl_dir
  42. */
  43. Public $ ctrl_dir = array ();
  44. /**
  45. * End of central directory record
  46. *
  47. * @ Public string $ eof_ctrl_dir
  48. */
  49. Public $ eof_ctrl_dir = "\ x50 \ x4b \ x05 \ x06 \ x00 \ x00 \ x00 \ x00 ";
  50. /**
  51. * Last offset position
  52. *
  53. * @ Public integer $ old_offset
  54. */
  55. Public $ old_offset = 0;
  56. /**
  57. * Converts an Unix timestamp to a four byte DOS date and time format (date
  58. * In high two bytes, time in low two bytes allowing magn1_comparison ).
  59. *
  60. * @ Param integer the current Unix timestamp
  61. *
  62. * @ Return integer the current date in a four byte DOS format
  63. *
  64. * @ Access private
  65. */
  66. Function unix2DosTime ($ unixtime = 0 ){
  67. $ Timearray = ($ unixtime = 0 )? Getdate (): getdate ($ unixtime );
  68. If ($ timearray ['Year'] <1980 ){
  69. $ Timearray ['Year'] = 1980;
  70. $ Timearray ['mon'] = 1;
  71. $ Timearray ['mday'] = 1;
  72. $ Timearray ['urs'] = 0;
  73. $ Timearray ['minutes '] = 0;
  74. $ Timearray ['seconds'] = 0;
  75. } // End if
  76. Return ($ timearray ['Year']-1980) <25) | ($ timearray ['mon'] <21) | ($ timearray ['mday'] <16) |
  77. ($ Timearray ['urs'] <11) | ($ timearray ['minutes '] <5) | ($ timearray ['seconds']> 1 );
  78. } // End of the 'unix2dostime () 'method
  79. /**
  80. * Adds "file" to archive
  81. *
  82. * @ Param string file contents
  83. * @ Param string name of the file in the archive (may contains the path)
  84. * @ Param integer the current timestamp
  85. *
  86. * @ Access public
  87. */
  88. Function addFile ($ data, $ name, $ time = 0)
  89. {
  90. $ Name = str_replace ('\', '/', $ name );
  91. $ Dtime = dechex ($ this-> unix2DosTime ($ time ));
  92. $ Hexdtime = '\ X'. $ dtime [6]. $ dtime [7]
  93. . '\ X'. $ dtime [4]. $ dtime [5]
  94. . '\ X'. $ dtime [2]. $ dtime [3]
  95. . '\ X'. $ dtime [0]. $ dtime [1];
  96. Eval ('$ hexdtime = "'. $ hexdtime .'";');
  97. $ Fr = "\ x50 \ x4b \ x03 \ x04 ";
  98. $ Fr. = "\ x14 \ x00"; // ver needed to extract
  99. $ Fr. = "\ x00 \ x00"; // gen purpose bit flag
  100. $ Fr. = "\ x08 \ x00"; // compression method
  101. $ Fr. = $ hexdtime; // last mod time and date
  102. // "Local file header" segment
  103. $ Unc_len = strlen ($ data );
  104. $ Crc = crc32 ($ data );
  105. $ Zdata = gzcompress ($ data );
  106. $ Zdata = substr ($ zdata, 0, strlen ($ zdata)-4), 2); // fix the crc bug
  107. $ C_len = strlen ($ zdata );
  108. $ Fr. = pack ('v', $ crc); // crc32
  109. $ Fr. = pack ('v', $ c_len); // compressed filesize
  110. $ Fr. = pack ('v', $ unc_len); // uncompressed filesize
  111. $ Fr. = pack ('v', strlen ($ name); // length of filename
  112. $ Fr. = pack ('v', 0); // extra field length
  113. $ Fr. = $ name;
  114. // "File data" segment
  115. $ Fr. = $ zdata;
  116. // "Data descriptor" segment (optional but necessary if archive is not
  117. // Served as file)
  118. $ Fr. = pack ('v', $ crc); // crc32
  119. $ Fr. = pack ('v', $ c_len); // compressed filesize
  120. $ Fr. = pack ('v', $ unc_len); // uncompressed filesize
  121. // Add this entry to array
  122. $ This-> datasec [] = $ fr;
  123. // Now add to central directory record
  124. $ Cdrec = "\ x50 \ x4b \ x01 \ x02 ";
  125. $ Cdrec. = "\ x00 \ x00"; // version made
  126. $ Cdrec. = "\ x14 \ x00"; // version needed to extract
  127. $ Cdrec. = "\ x00 \ x00"; // gen purpose bit flag
  128. $ Cdrec. = "\ x08 \ x00"; // compression method
  129. $ Cdrec. = $ hexdtime; // last mod time & date
  130. $ Cdrec. = pack ('v', $ crc); // crc32
  131. $ Cdrec. = pack ('v', $ c_len); // compressed filesize
  132. $ Cdrec. = pack ('v', $ unc_len); // uncompressed filesize
  133. $ Cdrec. = pack ('v', strlen ($ name); // length of filename
  134. $ Cdrec. = pack ('v', 0); // extra field length
  135. $ Cdrec. = pack ('v', 0); // file comment length
  136. $ Cdrec. = pack ('v', 0); // disk number start
  137. $ Cdrec. = pack ('v', 0); // internal file attributes
  138. $ Cdrec. = pack ('v', 32); // external file attributes-'archive' bit set
  139. $ Cdrec. = pack ('v', $ this-> old_offset); // relative offset of local header
  140. $ This-> old_offset + = strlen ($ fr );
  141. $ Cdrec. = $ name;
  142. // Optional extra field, file comment goes here
  143. // Save to central directory
  144. $ This-> ctrl_dir [] = $ cdrec;
  145. } // End of the 'addfile () 'method
  146. /**
  147. * Dumps out file
  148. *
  149. * @ Return string the zipped file
  150. *
  151. * @ Access public
  152. */
  153. Function file ()
  154. {
  155. $ Data = implode ('', $ this-> datasec );
  156. $ Ctrldir = implode ('', $ this-> ctrl_dir );
  157. Return
  158. $ Data.
  159. $ Ctrldir.
  160. $ This-> eof_ctrl_dir.
  161. Pack ('v', sizeof ($ this-> ctrl_dir). // total # of entries "on this disk"
  162. Pack ('v', sizeof ($ this-> ctrl_dir). // total # of entries overall
  163. Pack ('v', strlen ($ ctrldir). // size of central dir
  164. Pack ('v', strlen ($ data). // offset to start of central dir
  165. "\ X00 \ x00"; //. zip file comment length
  166. } // End of the 'file () 'method
  167. /**
  168. * A Wrapper of original addFile Function
  169. *
  170. * Created By Hasin Hayder at 29th Jan, AM
  171. *
  172. * @ Param array An Array of files with relative/absolute path to be added in Zip File
  173. *
  174. * @ Access public
  175. */
  176. Function addFiles ($ files/* Only Pass Array */)
  177. {
  178. Foreach ($ files as $ file)
  179. {
  180. If (is_file ($ file) // directory check
  181. {
  182. $ Data = implode ("", file ($ file ));
  183. $ This-> addFile ($ data, $ file );
  184. }
  185. }
  186. }
  187. /**
  188. * A Wrapper of original file Function
  189. *
  190. * Created By Hasin Hayder at 29th Jan, AM
  191. *
  192. * @ Param string Output file name
  193. *
  194. * @ Access public
  195. */
  196. Function output ($ file)
  197. {
  198. $ Fp = fopen ($ file, "w ");
  199. Fwrite ($ fp, $ this-> file ());
  200. Fclose ($ fp );
  201. }
  202. } // End of the 'zipfile' class

Php, zip

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.