Cerate zip files PHP


/ Published in: PHP
Save to your folder(s)

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


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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.