You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
6.3 KiB

  1. <?php
  2. /**
  3. * Zip file creation class.
  4. * Makes zip files.
  5. * From phpmyadmin <http://tinyurl.com/fjpwx>
  6. *
  7. * Based on :
  8. *
  9. * http://www.zend.com/codex.php?id=535&single=1
  10. * By Eric Mueller <eric@themepark.com>
  11. *
  12. * http://www.zend.com/codex.php?id=470&single=1
  13. * by Denis125 <webmaster@atlant.ru>
  14. *
  15. * a patch from Peter Listiak <mlady@users.sourceforge.net> for last modified
  16. * date and time of the compressed file
  17. *
  18. * Official ZIP file format: http://www.pkware.com/appnote.txt
  19. *
  20. * @access public
  21. */
  22. class zipfile
  23. {
  24. /**
  25. * Array to store compressed data
  26. *
  27. * @var array $datasec
  28. */
  29. var $datasec = array();
  30. /**
  31. * Central directory
  32. *
  33. * @var array $ctrl_dir
  34. */
  35. var $ctrl_dir = array();
  36. /**
  37. * End of central directory record
  38. *
  39. * @var string $eof_ctrl_dir
  40. */
  41. var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
  42. /**
  43. * Last offset position
  44. *
  45. * @var integer $old_offset
  46. */
  47. var $old_offset = 0;
  48. /**
  49. * Converts an Unix timestamp to a four byte DOS date and time format (date
  50. * in high two bytes, time in low two bytes allowing magnitude comparison).
  51. *
  52. * @param integer the current Unix timestamp
  53. *
  54. * @return integer the current date in a four byte DOS format
  55. *
  56. * @access private
  57. */
  58. function unix2DosTime($unixtime = 0) {
  59. $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
  60. if ($timearray['year'] < 1980) {
  61. $timearray['year'] = 1980;
  62. $timearray['mon'] = 1;
  63. $timearray['mday'] = 1;
  64. $timearray['hours'] = 0;
  65. $timearray['minutes'] = 0;
  66. $timearray['seconds'] = 0;
  67. } // end if
  68. return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) |
  69. ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);
  70. } // end of the 'unix2DosTime()' method
  71. /**
  72. * Adds "file" to archive
  73. *
  74. * @param string file contents
  75. * @param string name of the file in the archive (may contains the path)
  76. * @param integer the current timestamp
  77. *
  78. * @access public
  79. */
  80. function addFile($data, $name, $time = 0)
  81. {
  82. $name = str_replace('\\', '/', $name);
  83. $dtime = dechex($this->unix2DosTime($time));
  84. $hexdtime = '\x' . $dtime[6] . $dtime[7]
  85. . '\x' . $dtime[4] . $dtime[5]
  86. . '\x' . $dtime[2] . $dtime[3]
  87. . '\x' . $dtime[0] . $dtime[1];
  88. eval('$hexdtime = "' . $hexdtime . '";');
  89. $fr = "\x50\x4b\x03\x04";
  90. $fr .= "\x14\x00"; // ver needed to extract
  91. $fr .= "\x00\x00"; // gen purpose bit flag
  92. $fr .= "\x08\x00"; // compression method
  93. $fr .= $hexdtime; // last mod time and date
  94. // "local file header" segment
  95. $unc_len = strlen($data);
  96. $crc = crc32($data);
  97. $zdata = gzcompress($data);
  98. $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
  99. $c_len = strlen($zdata);
  100. $fr .= pack('V', $crc); // crc32
  101. $fr .= pack('V', $c_len); // compressed filesize
  102. $fr .= pack('V', $unc_len); // uncompressed filesize
  103. $fr .= pack('v', strlen($name)); // length of filename
  104. $fr .= pack('v', 0); // extra field length
  105. $fr .= $name;
  106. // "file data" segment
  107. $fr .= $zdata;
  108. // "data descriptor" segment (optional but necessary if archive is not
  109. // served as file)
  110. // nijel(2004-10-19): this seems not to be needed at all and causes
  111. // problems in some cases (bug #1037737)
  112. //$fr .= pack('V', $crc); // crc32
  113. //$fr .= pack('V', $c_len); // compressed filesize
  114. //$fr .= pack('V', $unc_len); // uncompressed filesize
  115. // add this entry to array
  116. $this -> datasec[] = $fr;
  117. // now add to central directory record
  118. $cdrec = "\x50\x4b\x01\x02";
  119. $cdrec .= "\x00\x00"; // version made by
  120. $cdrec .= "\x14\x00"; // version needed to extract
  121. $cdrec .= "\x00\x00"; // gen purpose bit flag
  122. $cdrec .= "\x08\x00"; // compression method
  123. $cdrec .= $hexdtime; // last mod time & date
  124. $cdrec .= pack('V', $crc); // crc32
  125. $cdrec .= pack('V', $c_len); // compressed filesize
  126. $cdrec .= pack('V', $unc_len); // uncompressed filesize
  127. $cdrec .= pack('v', strlen($name) ); // length of filename
  128. $cdrec .= pack('v', 0 ); // extra field length
  129. $cdrec .= pack('v', 0 ); // file comment length
  130. $cdrec .= pack('v', 0 ); // disk number start
  131. $cdrec .= pack('v', 0 ); // internal file attributes
  132. $cdrec .= pack('V', 32 ); // external file attributes - 'archive' bit set
  133. $cdrec .= pack('V', $this -> old_offset ); // relative offset of local header
  134. $this -> old_offset += strlen($fr);
  135. $cdrec .= $name;
  136. // optional extra field, file comment goes here
  137. // save to central directory
  138. $this -> ctrl_dir[] = $cdrec;
  139. } // end of the 'addFile()' method
  140. /**
  141. * Dumps out file
  142. *
  143. * @return string the zipped file
  144. *
  145. * @access public
  146. */
  147. function file()
  148. {
  149. $data = implode('', $this -> datasec);
  150. $ctrldir = implode('', $this -> ctrl_dir);
  151. return
  152. $data .
  153. $ctrldir .
  154. $this -> eof_ctrl_dir .
  155. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries "on this disk"
  156. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries overall
  157. pack('V', strlen($ctrldir)) . // size of central dir
  158. pack('V', strlen($data)) . // offset to start of central dir
  159. "\x00\x00"; // .zip file comment length
  160. } // end of the 'file()' method
  161. } // end of the 'zipfile' class
  162. ?>