Pomoc - Szukaj - U¿ytkownicy - Kalendarz
Pe³na wersja: [php] Klasa Do Generowania Zip
Forum PHP.pl > Forum > PHP
hhg
Znalaz³em tak± klase ale ta generuje zip-y z b³êdami chociaz jest nawet na stronie Zedna w code. Oto kod ktory powinien wygenerowac zipa ktory mozna pobrac po klikniecu w link. Niestety tak jak wyzej z bledem (otwierane pod Windows)

  1. <?php 
  2.  
  3. /* 
  4.  
  5. Zip file creation class 
  6. makes zip files on the fly...
  7.  
  8. use the functions add_dir() and add_file() to build the zip file;
  9. see example code below 
  10.  
  11. by Eric Mueller
  12. <a href=\"http://www.themepark.com\" target=\"_blank\">http://www.themepark.com</a> 
  13.  
  14. v1.1 9-20-01
  15.   - added comments to example
  16.  
  17. v1.0 2-5-01
  18.  
  19. initial version with:
  20.   - class appearance
  21.   - add_file() and file() methods
  22.   - gzcompress() output hacking
  23. by Denis O.Philippov, webmaster@atlant.ru, <a href=\"http://www.atlant.ru\" target=\"_blank\">http://www.atlant.ru</a>
  24.  
  25. */ 
  26.  
  27. // official ZIP file format: <a href=\"http://www.pkware.com/appnote.txt\" target=\"_blank\">http://www.pkware.com/appnote.txt</a> 
  28.  
  29. class zipfile
  30. {
  31.  
  32. var $datasec = array(); // array to store compressed data 
  33. var $ctrl_dir = array(); // central directory  
  34. var $eof_ctrl_dir = "x50x4bx05x06x00x00x00x00"; //end of Central directory record 
  35. var $old_offset = 0; 
  36.  
  37. function add_dir($name)  
  38.  
  39. // adds "directory" to archive - do this before putting any files in directory! 
  40. // $name - name of directory... like this: "path/" 
  41. // ...then you can add files using add_file with names like "path/file.txt" 
  42. {
  43. $name = str_replace("\", "/", $name);
  44.  
  45. $fr = "x50x4bx03x04"; 
  46. $fr .= "x0ax00"; // ver needed to extract 
  47. $fr .= "x00x00"; // gen purpose bit flag 
  48. $fr .= "x00x00"; // compression method 
  49. $fr .= "x00x00x00x00"; // last mod time and date 
  50.  
  51. $fr .= pack("V",0); // crc32 
  52. $fr .= pack("V",0); //compressed filesize 
  53. $fr .= pack("V",0); //uncompressed filesize 
  54. $fr .= pack("v", strlen($name) ); //length of pathname 
  55. $fr .= pack("v", 0 ); //extra field length 
  56. $fr .= $name;
  57. // end of "local file header" segment 
  58.  
  59. // no "file data" segment for path 
  60.  
  61. // "data descriptor" segment (optional but necessary if archive is not served as file) 
  62. $fr .= pack("V",$crc); //crc32 
  63. $fr .= pack("V",$c_len); //compressed filesize 
  64. $fr .= pack("V",$unc_len); //uncompressed filesize 
  65.  
  66. // add this entry to array 
  67. $this -> datasec[] = $fr; 
  68.  
  69. $new_offset = strlen(implode("", $this->datasec)); 
  70.  
  71. // ext. file attributes mirrors MS-DOS directory attr byte, detailed 
  72. // at <a href=\"http://support.microsoft.com/support/kb/articles/Q125/0/19.asp\" target=\"_blank\">http://support.microsoft.com/support/kb/ar...s/Q125/0/19.asp</a> 
  73.  
  74. // now add to central record 
  75. $cdrec = "x50x4bx01x02"; 
  76. $cdrec .="x00x00"; // version made by 
  77. $cdrec .="x0ax00"; // version needed to extract 
  78. $cdrec .="x00x00"; // gen purpose bit flag 
  79. $cdrec .="x00x00"; // compression method 
  80. $cdrec .="x00x00x00x00"; // last mod time & date 
  81. $cdrec .= pack("V",0); // crc32 
  82. $cdrec .= pack("V",0); //compressed filesize 
  83. $cdrec .= pack("V",0); //uncompressed filesize 
  84. $cdrec .= pack("v", strlen($name) ); //length of filename 
  85. $cdrec .= pack("v", 0 ); //extra field length  
  86. $cdrec .= pack("v", 0 ); //file comment length 
  87. $cdrec .= pack("v", 0 ); //disk number start 
  88. $cdrec .= pack("v", 0 ); //internal file attributes 
  89. $ext = "x00x00x10x00"; 
  90. $ext = "xffxffxffxff";
  91. $cdrec .= pack("V", 16 ); //external file attributes - 'directory' bit set 
  92.  
  93. $cdrec .= pack("V", $this -> old_offset ); //relative offset of local header 
  94. $this -> old_offset = $new_offset; 
  95.  
  96. $cdrec .= $name;
  97. // optional extra field, file comment goes here 
  98. // save to array 
  99. $this -> ctrl_dir[] = $cdrec;
  100.  
  101.  
  102.  
  103.  
  104. function add_file($data, $name)  
  105.  
  106. // adds "file" to archive  
  107. // $data - file contents 
  108. // $name - name of file in archive. Add path if your want 
  109.  
  110. {
  111. $name = str_replace("", "/", $name);
  112. //$name = str_replace("", "", $name); 
  113.  
  114. $fr = "x50x4bx03x04"; 
  115. $fr .= "x14x00"; // ver needed to extract 
  116. $fr .= "x00x00"; // gen purpose bit flag 
  117. $fr .= "x08x00"; // compression method 
  118. $fr .= "x00x00x00x00"; // last mod time and date 
  119.  
  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. // end of "local file header" segment 
  132.  
  133. // "file data" segment 
  134. $fr .= $zdata;
  135.  
  136. // "data descriptor" segment (optional but necessary if archive is not 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.  
  144. $new_offset = strlen(implode("", $this->datasec)); 
  145.  
  146. // now add to central directory record 
  147. $cdrec = "x50x4bx01x02"; 
  148. $cdrec .="x00x00"; // version made by 
  149. $cdrec .="x14x00"; // version needed to extract 
  150. $cdrec .="x00x00"; // gen purpose bit flag 
  151. $cdrec .="x08x00"; // compression method 
  152. $cdrec .="x00x00x00x00"; // last mod time & date 
  153. $cdrec .= pack("V",$crc); // crc32 
  154. $cdrec .= pack("V",$c_len); //compressed filesize 
  155. $cdrec .= pack("V",$unc_len); //uncompressed filesize 
  156. $cdrec .= pack("v", strlen($name) ); //length of filename 
  157. $cdrec .= pack("v", 0 ); //extra field length  
  158. $cdrec .= pack("v", 0 ); //file comment length 
  159. $cdrec .= pack("v", 0 ); //disk number start 
  160. $cdrec .= pack("v", 0 ); //internal file attributes 
  161. $cdrec .= pack("V", 32 ); //external file attributes - 'archive' bit set 
  162.  
  163. $cdrec .= pack("V", $this -> old_offset ); //relative offset of local header 
  164. // echo "old offset is ".$this->old_offset.", new offset is $new_offset<br>"; 
  165. $this -> old_offset = $new_offset; 
  166.  
  167. $cdrec .= $name;
  168. // optional extra field, file comment goes here 
  169. // save to central directory 
  170. $this -> ctrl_dir[] = $cdrec;
  171.  
  172. function file() { // dump out file  
  173. $data = implode("", $this -> datasec);
  174. $ctrldir = implode("", $this -> ctrl_dir);
  175.  
  176. return  
  177. $data.
  178. $ctrldir.
  179. $this -> eof_ctrl_dir.
  180. pack("v", sizeof($this -> ctrl_dir)).  // total # of entries "on this disk
  181. pack("v", sizeof($this -> ctrl_dir)).  // total # of entries overall 
  182. pack("V", strlen($ctrldir)).  // size of central dir 
  183. pack("V", strlen($data)).  // offset to start of central dir 
  184. "x00x00";  // .zip file comment length 
  185. }
  186.  
  187.  
  188.  
  189. $zipfile = new zipfile();
  190.  
  191. // add the subdirectory ... important!
  192. $zipfile -> add_dir("dir/"); 
  193.  
  194. // add the binary data stored in the string 'filedata'
  195. $filedata = "(read your file into $filedata)";
  196. $zipfile -> add_file($filedata, "dir/file.txt");
  197.  
  198. /*
  199. // the next three lines force an immediate download of the zip file:
  200. header("Content-type: application/octet-stream");
  201. header("Content-disposition: attachment; filename=test.zip");
  202. echo $zipfile -> file();
  203. */
  204.  
  205. // OR instead of doing that, you can write out the file to the loca disk like this
    :
  206. $filename = "output.zip";
  207. $fd = fopen ($filename, "wb");
  208. $out = fwrite ($fd, $zipfile -> file());
  209. fclose ($fd);
  210.  
  211.  
  212.  
  213. ?>
  214.  
  215.  
  216.  
  217. // then offer it to the user to download:
  218. <a href="output.zip">Click here to download the new zip file.</a>
UDAT
Spróbuj skorzystaæ z PECL zip" title="Zobacz w manualu PHP" target="_manual
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.