zip.lib.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. *
  5. * @version $Id: zip.lib.php 10240 2007-04-01 11:02:46Z cybot_tm $
  6. */
  7. /**
  8. * Zip file creation class.
  9. * Makes zip files.
  10. *
  11. * Based on :
  12. *
  13. * http://www.zend.com/codex.php?id=535&single=1
  14. * By Eric Mueller <eric@themepark.com>
  15. *
  16. * http://www.zend.com/codex.php?id=470&single=1
  17. * by Denis125 <webmaster@atlant.ru>
  18. *
  19. * a patch from Peter Listiak <mlady@users.sourceforge.net> for last modified
  20. * date and time of the compressed file
  21. *
  22. * Official ZIP file format: http://www.pkware.com/appnote.txt
  23. *
  24. * @access public
  25. */
  26. class zipfile
  27. {
  28. /**
  29. * Array to store compressed data
  30. *
  31. * @var array $datasec
  32. */
  33. var $datasec = array();
  34. /**
  35. * Central directory
  36. *
  37. * @var array $ctrl_dir
  38. */
  39. var $ctrl_dir = array();
  40. /**
  41. * End of central directory record
  42. *
  43. * @var string $eof_ctrl_dir
  44. */
  45. var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
  46. /**
  47. * Last offset position
  48. *
  49. * @var integer $old_offset
  50. */
  51. var $old_offset = 0;
  52. /**
  53. * Converts an Unix timestamp to a four byte DOS date and time format (date
  54. * in high two bytes, time in low two bytes allowing magnitude comparison).
  55. *
  56. * @param integer the current Unix timestamp
  57. *
  58. * @return integer the current date in a four byte DOS format
  59. *
  60. * @access private
  61. */
  62. function unix2DosTime($unixtime = 0) {
  63. $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
  64. if ($timearray['year'] < 1980) {
  65. $timearray['year'] = 1980;
  66. $timearray['mon'] = 1;
  67. $timearray['mday'] = 1;
  68. $timearray['hours'] = 0;
  69. $timearray['minutes'] = 0;
  70. $timearray['seconds'] = 0;
  71. } // end if
  72. return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) |
  73. ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);
  74. } // end of the 'unix2DosTime()' method
  75. /**
  76. * Adds "file" to archive
  77. *
  78. * @param string file contents
  79. * @param string name of the file in the archive (may contains the path)
  80. * @param integer the current timestamp
  81. *
  82. * @access public
  83. */
  84. function addFile($data, $name, $time = 0)
  85. {
  86. $name = str_replace('\\', '/', $name);
  87. $dtime = dechex($this->unix2DosTime($time));
  88. $hexdtime = '\x' . $dtime[6] . $dtime[7]
  89. . '\x' . $dtime[4] . $dtime[5]
  90. . '\x' . $dtime[2] . $dtime[3]
  91. . '\x' . $dtime[0] . $dtime[1];
  92. eval('$hexdtime = "' . $hexdtime . '";');
  93. $fr = "\x50\x4b\x03\x04";
  94. $fr .= "\x14\x00"; // ver needed to extract
  95. $fr .= "\x00\x00"; // gen purpose bit flag
  96. $fr .= "\x08\x00"; // compression method
  97. $fr .= $hexdtime; // last mod time and date
  98. // "local file header" segment
  99. $unc_len = strlen($data);
  100. $crc = crc32($data);
  101. $zdata = gzcompress($data);
  102. $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
  103. $c_len = strlen($zdata);
  104. $fr .= pack('V', $crc); // crc32
  105. $fr .= pack('V', $c_len); // compressed filesize
  106. $fr .= pack('V', $unc_len); // uncompressed filesize
  107. $fr .= pack('v', strlen($name)); // length of filename
  108. $fr .= pack('v', 0); // extra field length
  109. $fr .= $name;
  110. // "file data" segment
  111. $fr .= $zdata;
  112. // "data descriptor" segment (optional but necessary if archive is not
  113. // served as file)
  114. // nijel(2004-10-19): this seems not to be needed at all and causes
  115. // problems in some cases (bug #1037737)
  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. } // end of the 'zipfile' class
  166. ?>