Posted By

nicolaspar on 07/13/06


Tagged


Versions (?)

Who likes this?

17 people have marked this snippet as a favorite

luman
Roshambo
mate
meth
alexsancho
javierarce
blakeb
clapfouine
fael
maid450
vali29
willcodeforfood
hellion
dave333
dinkan
mfigge
lfcortes


Php crear archivos zip


 / Published in: PHP
 

Ej: $ziper = new zipfile(); $filename1 = 'images/imagen1.jpg'; $filename2 = 'images/imagen2.jpg'; $fileZip = 'images/imagen.zip'; $ziper->addFile(filegetcontents($filename1),$filename1); $ziper->addFile(filegetcontents($filename2),$filename2); $ziper->output($fileZip);

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

Report this snippet  

You need to login to post a comment.