Posted By

ed209 on 08/26/09


Tagged

php zip phpmyadmin


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

NyX
umang_nine


PHP Zip Lib


 / Published in: PHP
 

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

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: wwwzealdcom on August 26, 2009

<hi>

You need to login to post a comment.