Поиграть


Фотографии знаменитостей, Герои нашего времени

Создаём zip в PHP на лету

Code (php)
  1.  
  2. <?php
  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. http://www.themepark.com
  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, http://www.atlant.ru
  24.  
  25. */
  26.  
  27. // official ZIP file format: http://www. // pkware.com/appnote.txt
  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 = "\x50\x4b\x05\x06\x00\x00\x00\x00"; //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 = "\x50\x4b\x03\x04";
  46.         $fr .= "\x0a\x00";    // ver needed to extract
  47.         $fr .= "\x00\x00";    // gen purpose bit flag
  48.         $fr .= "\x00\x00";    // compression method
  49.         $fr .= "\x00\x00\x00\x00"; // 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 http://support.microsoft.com/support/kb/articles/Q125/0/19.asp
  73.  
  74.         // now add to central record
  75.         $cdrec = "\x50\x4b\x01\x02";
  76.         $cdrec .="\x00\x00";    // version made by
  77.         $cdrec .="\x0a\x00";    // version needed to extract
  78.         $cdrec .="\x00\x00";    // gen purpose bit flag
  79.         $cdrec .="\x00\x00";    // compression method
  80.         $cdrec .="\x00\x00\x00\x00"; // 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 = "\x00\x00\x10\x00";
  90.         $ext = "\xff\xff\xff\xff"
  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.  
  105.     function add_file($data, $name)   
  106.  
  107.     // adds "file" to archive   
  108.     // $data - file contents
  109.     // $name - name of file in archive. Add path if your want
  110.  
  111.     { 
  112.         $name = str_replace("\\", "/", $name)
  113.         //$name = str_replace("\\", "\\\\", $name);
  114.  
  115.         $fr = "\x50\x4b\x03\x04";
  116.         $fr .= "\x14\x00";    // ver needed to extract
  117.         $fr .= "\x00\x00";    // gen purpose bit flag
  118.         $fr .= "\x08\x00";    // compression method
  119.         $fr .= "\x00\x00\x00\x00"; // last mod time and date
  120.  
  121.         $unc_len = strlen($data)
  122.         $crc = crc32($data)
  123.         $zdata = gzcompress($data)
  124.         $zdata = substr( substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
  125.         $c_len = strlen($zdata)
  126.         $fr .= pack("V",$crc); // crc32
  127.         $fr .= pack("V",$c_len); //compressed filesize
  128.         $fr .= pack("V",$unc_len); //uncompressed filesize
  129.         $fr .= pack("v", strlen($name) ); //length of filename
  130.         $fr .= pack("v", 0 ); //extra field length
  131.         $fr .= $name
  132.         // end of "local file header" segment
  133.          
  134.         // "file data" segment
  135.         $fr .= $zdata
  136.  
  137.         // "data descriptor" segment (optional but necessary if archive is not served as file)
  138.         $fr .= pack("V",$crc); //crc32
  139.         $fr .= pack("V",$c_len); //compressed filesize
  140.         $fr .= pack("V",$unc_len); //uncompressed filesize
  141.  
  142.         // add this entry to array
  143.         $this -> datasec[] = $fr;
  144.  
  145.         $new_offset = strlen(implode("", $this->datasec));
  146.  
  147.         // now add to central directory record
  148.         $cdrec = "\x50\x4b\x01\x02";
  149.         $cdrec .="\x00\x00";    // version made by
  150.         $cdrec .="\x14\x00";    // version needed to extract
  151.         $cdrec .="\x00\x00";    // gen purpose bit flag
  152.         $cdrec .="\x08\x00";    // compression method
  153.         $cdrec .="\x00\x00\x00\x00"; // last mod time & date
  154.         $cdrec .= pack("V",$crc); // crc32
  155.         $cdrec .= pack("V",$c_len); //compressed filesize
  156.         $cdrec .= pack("V",$unc_len); //uncompressed filesize
  157.         $cdrec .= pack("v", strlen($name) ); //length of filename
  158.         $cdrec .= pack("v", 0 ); //extra field length   
  159.         $cdrec .= pack("v", 0 ); //file comment length
  160.         $cdrec .= pack("v", 0 ); //disk number start
  161.         $cdrec .= pack("v", 0 ); //internal file attributes
  162.         $cdrec .= pack("V", 32 ); //external file attributes - ‘archive’ bit set
  163.  
  164.         $cdrec .= pack("V", $this -> old_offset ); //relative offset of local header
  165. //      &n // bsp; echo "old offset is ".$this->old_offset.", new offset is $new_offset
  166. ";
  167.         $this -> old_offset = $new_offset;
  168.  
  169.         $cdrec .= $name; 
  170.         // optional extra field, file comment goes here
  171.         // save to central directory
  172.         $this -> ctrl_dir[] = $cdrec; 
  173.     }
  174.  
  175.     function file() { // dump out file   
  176.         $data = implode("", $this -> datasec); 
  177.         $ctrldir = implode("", $this -> ctrl_dir); 
  178.  
  179.         return   
  180.             $data. 
  181.             $ctrldir. 
  182.             $this -> eof_ctrl_dir. 
  183.             pack("v", sizeof($this -> ctrl_dir)).     // total # of entries "on this disk"
  184.             pack("v", sizeof($this -> ctrl_dir)).     // total # of entries overall
  185.             pack("V", strlen($ctrldir)).             // size of central dir
  186.             pack("V", strlen($data)).                 // offset to start of central dir
  187.             "\x00\x00";                             // .zip file comment length
  188.     }
  189.  
  190. ?>
  191.  
  192. Example Usage of the Class
  193.  
  194. <?php
  195.  
  196. $zipfile = new zipfile(); 
  197.  
  198. // add the subdirectory … important!
  199. $zipfile -> add_dir("dir/");
  200.  
  201. // add the binary data stored in the string ‘filedata’
  202. $filedata = "(read your file into $filedata)"; 
  203. $zipfile -> add_file($filedata, "dir/file.txt"); 
  204.  
  205. // the next three lines force an immediate download of the zip file:
  206. header("Content-type: application/octet-stream"); 
  207. header("Content-disposition: attachment; filename=test.zip"); 
  208. echo $zipfile -> file(); 
  209.  
  210.  
  211. // OR instead of doing that, you can write out the file to the loca disk like this:
  212. $filename = "output.zip";
  213. $fd = fopen ($filename, "wb");
  214. $out = fwrite ($fd, $zipfile -> file());
  215. fclose ($fd);
  216.  
  217. // then offer it to the user to download:
  218. <a href="output.zip">Click here to download the new zip file.</a>
  219. ?>
  220.  

+1+2+3+4+5 (2 голосов, в среднем: 5 из 5)
Загрузка... Загрузка...

Оставьте комментарий

Блог разработчика / Болеет компьютер? удаление вирусов и вредоносных программ
spiele download spiele spiele spielen
Всё о Киеве: ВВиртуальные прогулки по Киеву. Новости, Достопримечательности. Полезные сведения.
swad at eFoodDepot.com