Rumah > pembangunan bahagian belakang > tutorial php > 一个php把一组文件打包成zip的类

一个php把一组文件打包成zip的类

WBOY
Lepaskan: 2016-07-25 08:43:06
asal
864 orang telah melayarinya

这段php类可以挨个添加文件到数组,最后将添加的文件打包成zip

  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 By :
  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 magnitude 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'] $timearray['year'] = 1980;
  69. $timearray['mon'] = 1;
  70. $timearray['mday'] = 1;
  71. $timearray['hours'] = 0;
  72. $timearray['minutes'] = 0;
  73. $timearray['seconds'] = 0;
  74. } // end if
  75. return (($timearray['year'] - 1980) ($timearray['hours'] > 1);
  76. } // end of the 'unix2DosTime()' method
  77. /**
  78. * Adds "file" to archive
  79. *
  80. * @param string file contents
  81. * @param string name of the file in the archive (may contains the path)
  82. * @param integer the current timestamp
  83. *
  84. * @access public
  85. */
  86. function addFile($data, $name, $time = 0)
  87. {
  88. $name = str_replace('\\', '/', $name);
  89. $dtime = dechex($this->unix2DosTime($time));
  90. $hexdtime = '\x' . $dtime[6] . $dtime[7]
  91. . '\x' . $dtime[4] . $dtime[5]
  92. . '\x' . $dtime[2] . $dtime[3]
  93. . '\x' . $dtime[0] . $dtime[1];
  94. eval('$hexdtime = "' . $hexdtime . '";');
  95. $fr = "\x50\x4b\x03\x04";
  96. $fr .= "\x14\x00"; // ver needed to extract
  97. $fr .= "\x00\x00"; // gen purpose bit flag
  98. $fr .= "\x08\x00"; // compression method
  99. $fr .= $hexdtime; // last mod time and date
  100. // "local file header" segment
  101. $unc_len = strlen($data);
  102. $crc = crc32($data);
  103. $zdata = gzcompress($data);
  104. $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
  105. $c_len = strlen($zdata);
  106. $fr .= pack('V', $crc); // crc32
  107. $fr .= pack('V', $c_len); // compressed filesize
  108. $fr .= pack('V', $unc_len); // uncompressed filesize
  109. $fr .= pack('v', strlen($name)); // length of filename
  110. $fr .= pack('v', 0); // extra field length
  111. $fr .= $name;
  112. // "file data" segment
  113. $fr .= $zdata;
  114. // "data descriptor" segment (optional but necessary if archive is not
  115. // served as file)
  116. $fr .= pack('V', $crc); // crc32
  117. $fr .= pack('V', $c_len); // compressed filesize
  118. $fr .= pack('V', $unc_len); // uncompressed filesize
  119. // add this entry to array
  120. $this -> datasec[] = $fr;
  121. // now add to central directory record
  122. $cdrec = "\x50\x4b\x01\x02";
  123. $cdrec .= "\x00\x00"; // version made by
  124. $cdrec .= "\x14\x00"; // version needed to extract
  125. $cdrec .= "\x00\x00"; // gen purpose bit flag
  126. $cdrec .= "\x08\x00"; // compression method
  127. $cdrec .= $hexdtime; // last mod time & date
  128. $cdrec .= pack('V', $crc); // crc32
  129. $cdrec .= pack('V', $c_len); // compressed filesize
  130. $cdrec .= pack('V', $unc_len); // uncompressed filesize
  131. $cdrec .= pack('v', strlen($name) ); // length of filename
  132. $cdrec .= pack('v', 0 ); // extra field length
  133. $cdrec .= pack('v', 0 ); // file comment length
  134. $cdrec .= pack('v', 0 ); // disk number start
  135. $cdrec .= pack('v', 0 ); // internal file attributes
  136. $cdrec .= pack('V', 32 ); // external file attributes - 'archive' bit set
  137. $cdrec .= pack('V', $this -> old_offset ); // relative offset of local header
  138. $this -> old_offset += strlen($fr);
  139. $cdrec .= $name;
  140. // optional extra field, file comment goes here
  141. // save to central directory
  142. $this -> ctrl_dir[] = $cdrec;
  143. } // end of the 'addFile()' method
  144. /**
  145. * Dumps out file
  146. *
  147. * @return string the zipped file
  148. *
  149. * @access public
  150. */
  151. function file()
  152. {
  153. $data = implode('', $this -> datasec);
  154. $ctrldir = implode('', $this -> ctrl_dir);
  155. return
  156. $data .
  157. $ctrldir .
  158. $this -> eof_ctrl_dir .
  159. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries "on this disk"
  160. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries overall
  161. pack('V', strlen($ctrldir)) . // size of central dir
  162. pack('V', strlen($data)) . // offset to start of central dir
  163. "\x00\x00"; // .zip file comment length
  164. } // end of the 'file()' method
  165. /**
  166. * A Wrapper of original addFile Function
  167. *
  168. * Created By Hasin Hayder at 29th Jan, 1:29 AM
  169. *
  170. * @param array An Array of files with relative/absolute path to be added in Zip File
  171. *
  172. * @access public
  173. */
  174. function addFiles($files /*Only Pass Array*/)
  175. {
  176. foreach($files as $file)
  177. {
  178. if (is_file($file)) //directory check
  179. {
  180. $data = implode("",file($file));
  181. $this->addFile($data,$file);
  182. }
  183. }
  184. }
  185. /**
  186. * A Wrapper of original file Function
  187. *
  188. * Created By Hasin Hayder at 29th Jan, 1:29 AM
  189. *
  190. * @param string Output file name
  191. *
  192. * @access public
  193. */
  194. function output($file)
  195. {
  196. $fp=fopen($file,"w");
  197. fwrite($fp,$this->file());
  198. fclose($fp);
  199. }
  200. } // end of the 'zipfile' class
复制代码

php, zip


sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan