Pomoc - Szukaj - U¿ytkownicy - Kalendarz
Pe³na wersja: Dodawanie do ZIP'a
Forum PHP.pl > Forum > PHP
Beenn
Witam.
Potrzebuje do pliku ZIP generowanego przez php dodac pliki znajdujace sie na serwerze. Z phpMyAdmin wyciagnolem klase obslugujaca ZIPy...

zip.lib.php:
  1. <?php
  2. /* $Id: zip.lib.php,v 1.7 2002/10/23 04:17:26 robbat2 Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5.  
  6. /**
  7.  * Zip file creation class.
  8.  * Makes zip files.
  9.  *
  10.  * Based on :
  11.  *
  12.  * http://www.zend.com/codex.php?id=535&single=1
  13.  * By Eric Mueller <eric@themepark.com>
  14.  *
  15.  * http://www.zend.com/codex.php?id=470&single=1
  16.  * by Denis125 <webmaster@atlant.ru>
  17.  *
  18.  * a patch from Peter Listiak <mlady@users.sourceforge.net> for last modified
  19.  * date and time of the compressed file
  20.  *
  21.  * Official ZIP file format: http://www.pkware.com/appnote.txt
  22.  *
  23.  * @access public
  24.  */
  25. class zipfile
  26. {
  27. /**
  28.  * Array to store compressed data
  29.  *
  30.  * @var array $datasec
  31.  */
  32. var $datasec = array();
  33.  
  34. /**
  35.  * Central directory
  36.  *
  37.  * @var array $ctrl_dir
  38.  */
  39. var $ctrl_dir  = array();
  40.  
  41. /**
  42.  * End of central directory record
  43.  *
  44.  * @var string  $eof_ctrl_dir
  45.  */
  46. var $eof_ctrl_dir = &#092;"x50x4bx05x06x00x00x00x00\";
  47.  
  48. /**
  49.  * Last offset position
  50.  *
  51.  * @var integer $old_offset
  52.  */
  53. var $old_offset  = 0;
  54.  
  55.  
  56. /**
  57.  * Converts an Unix timestamp to a four byte DOS date and time format (date
  58.  * in high two bytes, time in low two bytes allowing magnitude comparison).
  59.  *
  60.  * @param integer the current Unix timestamp
  61.  *
  62.  * @return integer the current date in a four byte DOS format
  63.  *
  64.  * @access private
  65.  */
  66. function unix2DosTime($unixtime = 0) {
  67. $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
  68.  
  69. if ($timearray['year'] < 1980) {
  70. $timearray['year'] = 1980;
  71. $timearray['mon']  = 1;
  72. $timearray['mday'] = 1;
  73. $timearray['hours']  = 0;
  74. $timearray['minutes'] = 0;
  75. $timearray['seconds'] = 0;
  76. } // end if
  77.  
  78. return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) |
  79. ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);
  80. } // end of the 'unix2DosTime()' method
  81.  
  82.  
  83. /**
  84.  * Adds \"file\" to archive
  85.  *
  86.  * @param string  file contents
  87.  * @param string  name of the file in the archive (may contains the path)
  88.  * @param integer the current timestamp
  89.  *
  90.  * @access public
  91.  */
  92. function add_file($data, $name)
  93.  
  94. // adds \"file\" to archive
  95. // $data - file contents 
  96. // $name - name of file in archive. Add path if your want 
  97.  
  98. {  
  99. //$name = str_replace(\"\", \"/\", $name);  
  100. $name = str_replace(&#092;"\", \"/\", $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  = &#092;"x50x4bx03x04\";
  110. $fr  .= &#092;"x14x00\"; // ver needed to extract
  111. $fr  .= &#092;"x00x00\"; // gen purpose bit flag
  112. $fr  .= &#092;"x08x00\"; // compression method
  113. $fr  .= $hexdtime;  // last mod time and date 
  114.  
  115. $unc_len = strlen($data);
  116. $crc  = crc32($data);
  117. $zdata  = gzcompress($data);
  118. $zdata  = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
  119. $c_len  = strlen($zdata);
  120. $fr .= pack('V', $crc);  // crc32
  121. $fr .= pack('V', $c_len);  // compressed filesize
  122. $fr .= pack('V', $unc_len);  // uncompressed filesize
  123. $fr .= pack('v', strlen($name)); // length of filename
  124. $fr .= pack('v', 0); // extra field length
  125. $fr .= $name;
  126. // end of \"local file header\" segment 
  127.  
  128. // \"file data\" segment 
  129. $fr .= $zdata;  
  130.  
  131. // \"data descriptor\" segment (optional but necessary if archive is not served as file) 
  132. $fr .= pack('V', $crc);  // crc32
  133. $fr .= pack('V', $c_len);  // compressed filesize
  134. $fr .= pack('V', $unc_len);  // uncompressed filesize
  135.  
  136. // add this entry to array 
  137. $this -> datasec[] = $fr;
  138. $new_offset = strlen(implode('', $this->datasec));
  139.  
  140. // now add to central directory record 
  141. $cdrec = &#092;"x50x4bx01x02\";
  142. $cdrec .= &#092;"x00x00\"; // version made by
  143. $cdrec .= &#092;"x14x00\"; // version needed to extract
  144. $cdrec .= &#092;"x00x00\"; // gen purpose bit flag
  145. $cdrec .= &#092;"x08x00\"; // 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 = $new_offset;
  159.  
  160. $cdrec .= $name;
  161.  
  162. // optional extra field, file comment goes here
  163. // save to central directory
  164. $this -> ctrl_dir[] = $cdrec;  
  165. } 
  166.  
  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. &#092;"x00x00\";  // .zip file comment length
  190. } // end of the 'file()' method
  191.  
  192. } // end of the 'zipfile' class
  193. ?>


Niestety potrafie jedynie zapisac do tego ZIPa pliki txt...
test.php:
  1. <?php
  2. require('zip.lib.php');
  3.  
  4. $zipfile = new zipfile();  
  5.  
  6. // add the binary data stored in the string 'filedata' 
  7. $filedata = &#092;"jakis text\";  
  8. $zipfile -> add_file($filedata, &#092;"file.txt\");  
  9.  
  10. echo $zipfile -> file();  
  11.  
  12.  
  13. // OR instead of doing that, you can write out the file to the loca disk like this
  14. $filename = &#092;"output.zip\"; 
  15. $fd = fopen ($filename, &#092;"wb\"); 
  16. $out = fwrite ($fd, $zipfile -> file()); 
  17. fclose ($fd); 
  18.  
  19. // then offer it to the user to download: 
  20.  
  21. ?>
  22. <a href=\"output.zip\">Click here to download the new zip file.</a>


i teraz pytanie:
Czy ktos potrafilby napisac mi jak mozna dodac do ZIPa pliki pdf?? Nie koniecznie za pomoca tej klasy... jesli ktos ma jakis inny gotowy skrypt i chcialby sie podzielic chetnie zobacze smile.gif
aleksander
  1. <?php
  2. require('zip.lib.php');
  3.  
  4. $zipfile = new zipfile();  
  5.  
  6. // add the binary data stored in the string 'filedata'
  7. $filedata = file_get_contents( 'plik.pdf' );;  
  8. $zipfile -> add_file($filedata, &#092;"plik.pdf\");  
  9.  
  10. echo $zipfile -> file();  
  11.  
  12.  
  13. // OR instead of doing that, you can write out the file to the loca disk like this
    :
  14. $filename = &#092;"output.zip\";
  15. $fd = fopen ($filename, &#092;"wb\");
  16. $out = fwrite ($fd, $zipfile -> file());
  17. fclose ($fd);
  18.  
  19. // then offer it to the user to download:
  20.  
  21. ?>
  22. <a href=\"output.zip\">Click here to download the new zip file.</a>
Beenn
Dziala.
WIELKIE Dzieki!!
To jest wersja lo-fi g³ównej zawarto¶ci. Aby zobaczyæ pe³n± wersjê z wiêksz± zawarto¶ci±, obrazkami i formatowaniem proszê kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.