PHP packages a group of files as a ZIP archive class

Source: Internet
Author: User
Tags bit set crc32 time and date
  1. /**
  2. * Zip file Creation class.
  3. * Makes zip files.
  4. *
  5. * @access Public
  6. */
  7. Class ZipFile
  8. {
  9. /**
  10. * Array to store compressed data
  11. *
  12. * @public Array $datasec
  13. */
  14. Public $datasec = Array ();
  15. /**
  16. * Central Directory
  17. *
  18. * @public Array $ctrl _dir
  19. */
  20. Public $ctrl _dir = Array ();
  21. /**
  22. * End of Central directory record
  23. *
  24. * @public string $eof _ctrl_dir
  25. */
  26. Public $eof _ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
  27. /**
  28. * Last offset position
  29. *
  30. * @public integer $old _offset
  31. */
  32. Public $old _offset = 0;
  33. /**
  34. * Converts a Unix timestamp to a four byte DOS date and time format (date
  35. * In high and bytes, time in low, bytes allowing magnitude comparison).
  36. *
  37. * @param integer The current Unix timestamp
  38. *
  39. * @return Integer The current date in a four byte DOS format
  40. *
  41. * @access Private
  42. */
  43. function Unix2dostime ($unixtime = 0) {
  44. $timearray = ($unixtime = = 0)? GETDATE (): getdate ($unixtime);
  45. if ($timearray [' Year '] < 1980) {
  46. $timearray [' year '] = 1980;
  47. $timearray [' mon '] = 1;
  48. $timearray [' mday '] = 1;
  49. $timearray [' hours '] = 0;
  50. $timearray [' minutes '] = 0;
  51. $timearray [' seconds '] = 0;
  52. }//End If
  53. Return (($timearray [' Year ']-1980) << 25) | ($timearray [' mon '] << 21) | ($timearray [' Mday '] << 16) |
  54. ($timearray [' hours '] << 11) | ($timearray [' minutes '] << 5) | ($timearray [' seconds '] >> 1);
  55. }//End of the ' unix2dostime () ' method
  56. /**
  57. * Adds "file" to archive
  58. *
  59. * @param string file contents
  60. * @param string name of the the file in the archive (may contains the path)
  61. * @param integer The current timestamp
  62. *
  63. * @access Public
  64. */
  65. function AddFile ($data, $name, $time = 0)
  66. {
  67. $name = str_replace (' \ \ ', '/', $name);
  68. $dtime = Dechex ($this->unix2dostime ($time));
  69. $hexdtime = ' \x '. $dtime [6]. $dtime [7]
  70. . ' \x '. $dtime [4]. $dtime [5]
  71. . ' \x '. $dtime [2]. $dtime [3]
  72. . ' \x '. $dtime [0]. $dtime [1];
  73. Eval (' $hexdtime = '. $hexdtime. '";');
  74. $FR = "\x50\x4b\x03\x04";
  75. $fr. = "\x14\x00"; Ver needed to extract
  76. $fr. = "\x00\x00"; Gen Purpose bit Flag
  77. $fr. = "\x08\x00"; Compression method
  78. $fr. = $hexdtime; Last mod time and date
  79. "Local File Header" segment
  80. $unc _len = strlen ($data);
  81. $CRC = CRC32 ($data);
  82. $zdata = gzcompress ($data);
  83. $zdata = substr (substr ($zdata, 0, strlen ($zdata)-4), 2); Fix CRC Bug
  84. $c _len = strlen ($zdata);
  85. $fr. = Pack (' V ', $CRC); Crc32
  86. $fr. = Pack (' V ', $c _len); Compressed FileSize
  87. $fr. = Pack (' V ', $unc _len); Uncompressed FileSize
  88. $fr. = Pack (' V ', strlen ($name)); Length of filename
  89. $fr. = Pack (' V ', 0); Extra field length
  90. $fr. = $name;
  91. "File Data" segment
  92. $fr. = $zdata;
  93. "Data Descriptor" segment (optional but necessary if archive are not
  94. served as file)
  95. $fr. = Pack (' V ', $CRC); Crc32
  96. $fr. = Pack (' V ', $c _len); Compressed FileSize
  97. $fr. = Pack (' V ', $unc _len); Uncompressed FileSize
  98. Add this entry to array
  99. $this-datasec[] = $FR;
  100. Now add to Central directory record
  101. $cdrec = "\x50\x4b\x01\x02";
  102. $cdrec. = "\x00\x00"; Version made by
  103. $cdrec. = "\x14\x00"; Version needed to extract
  104. $cdrec. = "\x00\x00"; Gen Purpose bit Flag
  105. $cdrec. = "\x08\x00"; Compression method
  106. $cdrec. = $hexdtime; Last MoD time & date
  107. $cdrec. = Pack (' V ', $CRC); Crc32
  108. $cdrec. = Pack (' V ', $c _len); Compressed FileSize
  109. $cdrec. = Pack (' V ', $unc _len); Uncompressed FileSize
  110. $cdrec. = Pack (' V ', strlen ($name)); Length of filename
  111. $cdrec. = Pack (' V ', 0); Extra field length
  112. $cdrec. = Pack (' V ', 0); File Comment length
  113. $cdrec. = Pack (' V ', 0); Disk number Start
  114. $cdrec. = Pack (' V ', 0); Internal file attributes
  115. $cdrec. = Pack (' V ', 32); External file attributes-' archive ' bit set
  116. $cdrec. = Pack (' V ', $this-old_offset); Relative offset of local header
  117. $this-Old_offset + = strlen ($FR);
  118. $cdrec. = $name;
  119. Optional extra field, file comment goes here
  120. Save to Central directory
  121. $this-ctrl_dir[] = $cdrec;
  122. }//End of the ' AddFile () ' method
  123. /**
  124. * Dumps out file
  125. *
  126. * @return String The zipped file
  127. *
  128. * @access Public
  129. */
  130. function file ()
  131. {
  132. $data = Implode (", $this-datasec);
  133. $ctrldir = Implode (", $this-Ctrl_dir);
  134. Return
  135. $data.
  136. $ctrldir.
  137. Eof_ctrl_dir, $this.
  138. Pack (' V ', sizeof ($this-Ctrl_dir)). Total # of Entries "on this disk"
  139. Pack (' V ', sizeof ($this-Ctrl_dir)). Total # of entries overall
  140. Pack (' V ', strlen ($ctrldir)). Size of Central Dir
  141. Pack (' V ', strlen ($data)). Offset to start of the central Dir
  142. "\x00\x00"; . zip file Comment length
  143. }//end of the ' file () ' method
  144. /**
  145. * A Wrapper of original AddFile Function
  146. *
  147. * Created by Hasin Hayder at 29th Jan, 1:29 AM
  148. *
  149. * @param array An array of files with Relative/absolute path to being added in Zip File
  150. *
  151. * @access Public
  152. */
  153. function AddFiles ($files/*only Pass array*/)
  154. {
  155. foreach ($files as $file)
  156. {
  157. if (Is_file ($file))//directory check
  158. {
  159. $data = Implode ("", File ($file));
  160. $this->addfile ($data, $file);
  161. }
  162. }
  163. }
  164. /**
  165. * A Wrapper of original file Function
  166. *
  167. * Created by Hasin Hayder at 29th Jan, 1:29 AM
  168. *
  169. * @param string Output file name
  170. *
  171. * @access Public
  172. */
  173. function output ($file)
  174. {
  175. $FP =fopen ($file, "w");
  176. Fwrite ($fp, $this->file ());
  177. Fclose ($FP);
  178. }
  179. }//End of the ' ZipFile ' class
Copy Code
  • 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.