php打包一组文件为zip压缩包的类

原创
2016-07-25 08:56:20 563浏览
  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 an Unix timestamp to a four byte DOS date and time format (date
  35. * in high two bytes, time in low two 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 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('\\', '//m.sbmmt.com/m/', $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 is 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. $this -> eof_ctrl_dir .
  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 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 be 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
复制代码


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
上一条:php中time(),date(),mktime()的区别详解 下一条:PHP 获取网页源代码、网页标题的实现代码

相关文章

查看更多