Text Resizer Text Resizer
July 30th, 2010
You're browsing: Scriptmatico.Com » PHP » Crear archivos ZIP con PHP (Otra forma)

Crear archivos ZIP con PHP (Otra forma)

Posted on Nov 05 in PHPby adminPrintText Resizer Text Resizer

Script que te permite Crear achivo ZIP, en tu hosting Linux, el cual resulta de gran utilidad por a la hora de comprimir archivos o ficheros en nuestro serve, para asi ahorranos espacio de disco duro, y reducir tambien la transferencia - cortesia de http://www.themepark.com - aqui esta el codigo:

PHP:
  1. <?php
  2. /*
  3. Zip file creation class
  4. makes zip files on the fly...
  5. use the functions add_dir() and add_file() to build the zip file;
  6. see example code below
  7. by Eric Mueller
  8. http://www.themepark.com
  9. v1.1 9-20-01
  10. - added comments to example
  11. v1.0 2-5-01
  12. initial version with:
  13. - class appearance
  14. - add_file() and file() methods
  15. - gzcompress() output hacking
  16. by Denis O.Philippov, webmaster@atlant.ru, http://www.atlant.ru
  17. */
  18. // official ZIP file format: http://www.pkware.com/appnote.txt
  19.  
  20. class zipfile
  21. {
  22. var $datasec = array(); // array to store compressed data
  23. var $ctrl_dir = array(); // central directory
  24. var $eof_ctrl_dir = "x50x4bx05x06x00x00x00x00"; //end of Central directory record
  25. var $old_offset = 0;
  26.  
  27. function add_dir($name)
  28.  
  29. // adds "directory" to archive - do this before putting any files in directory!
  30. // $name - name of directory... like this: "path/"
  31. // ...then you can add files using add_file with names like "path/file.txt"
  32. {
  33. $name = str_replace("", "/", $name);
  34.  
  35. $fr = "x50x4bx03x04";
  36. $fr .= "x0ax00"; // ver needed to extract
  37. $fr .= "x00x00"; // gen purpose bit flag
  38. $fr .= "x00x00"; // compression method
  39. $fr .= "x00x00x00x00"; // last mod time and date
  40. $fr .= pack("V",0); // crc32
  41. $fr .= pack("V",0); //compressed filesize
  42. $fr .= pack("V",0); //uncompressed filesize
  43. $fr .= pack("v", strlen($name) ); //length of pathname
  44. $fr .= pack("v", 0 ); //extra field length
  45. $fr .= $name;
  46. // end of "local file header" segment
  47.  
  48. // no "file data" segment for path
  49.  
  50. // "data descriptor" segment (optional but necessary if archive is not served as file)
  51. $fr .= pack("V",$crc); //crc32
  52. $fr .= pack("V",$c_len); //compressed filesize
  53. $fr .= pack("V",$unc_len); //uncompressed filesize
  54.  
  55. // add this entry to array
  56. $this -> datasec[] = $fr;
  57. $new_offset = strlen(implode("", $this->datasec));
  58.  
  59. // ext. file attributes mirrors MS-DOS directory attr byte, detailed
  60. // at http://support.microsoft.com/support/kb/articles/Q125/0/19.asp
  61.  
  62. // now add to central record
  63. $cdrec = "x50x4bx01x02";
  64. $cdrec .="x00x00"; // version made by
  65. $cdrec .="x0ax00"; // version needed to extract
  66. $cdrec .="x00x00"; // gen purpose bit flag
  67. $cdrec .="x00x00"; // compression method
  68. $cdrec .="x00x00x00x00"; // last mod time & date
  69. $cdrec .= pack("V",0); // crc32
  70. $cdrec .= pack("V",0); //compressed filesize
  71. $cdrec .= pack("V",0); //uncompressed filesize
  72. $cdrec .= pack("v", strlen($name) ); //length of filename
  73. $cdrec .= pack("v", 0 ); //extra field length
  74. $cdrec .= pack("v", 0 ); //file comment length
  75.  
  76. $cdrec .= pack("v", 0 ); //disk number start
  77. $cdrec .= pack("v", 0 ); //internal file attributes
  78.  
  79. $ext = "x00x00x10x00";
  80. $ext = "xffxffxffxff";
  81. $cdrec .= pack("V", 16 ); //external file attributes - 'directory' bit set
  82.  
  83. $cdrec .= pack("V", $this -> old_offset ); //relative offset of local header
  84. $this -> old_offset = $new_offset;
  85. $cdrec .= $name;
  86. // optional extra field, file comment goes here
  87. // save to array
  88. $this -> ctrl_dir[] = $cdrec;
  89. }
  90. function add_file($data, $name)
  91. // adds "file" to archive
  92. // $data - file contents
  93. // $name - name of file in archive. Add path if your want
  94. {
  95. $name = str_replace("", "/", $name);
  96. //$name = str_replace("", "", $name);
  97. $fr = "x50x4bx03x04";
  98. $fr .= "x14x00"; // ver needed to extract
  99. $fr .= "x00x00"; // gen purpose bit flag
  100. $fr .= "x08x00"; // compression method
  101. $fr .= "x00x00x00x00"; // last mod time and date
  102.  
  103. $unc_len = strlen($data);
  104. $crc = crc32($data);
  105. $zdata = gzcompress($data);
  106. $zdata = substr( substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
  107. $c_len = strlen($zdata);
  108. $fr .= pack("V",$crc); // crc32
  109. $fr .= pack("V",$c_len); //compressed filesize
  110. $fr .= pack("V",$unc_len); //uncompressed filesize
  111. $fr .= pack("v", strlen($name) ); //length of filename
  112. $fr .= pack("v", 0 ); //extra field length
  113. $fr .= $name;
  114. // end of "local file header" segment
  115. // "file data" segment
  116. $fr .= $zdata;
  117.  
  118. // "data descriptor" segment (optional but necessary if archive is not served as file)
  119. $fr .= pack("V",$crc); //crc32
  120. $fr .= pack("V",$c_len); //compressed filesize
  121. $fr .= pack("V",$unc_len); //uncompressed filesize
  122.  
  123. // add this entry to array
  124. $this -> datasec[] = $fr;
  125. $new_offset = strlen(implode("", $this->datasec));
  126.  
  127. // now add to central directory record
  128. $cdrec = "x50x4bx01x02";
  129. $cdrec .="x00x00"; // version made by
  130. $cdrec .="x14x00"; // version needed to extract
  131. $cdrec .="x00x00"; // gen purpose bit flag
  132. $cdrec .="x08x00"; // compression method
  133. $cdrec .="x00x00x00x00"; // last mod time & date
  134. $cdrec .= pack("V",$crc); // crc32
  135. $cdrec .= pack("V",$c_len); //compressed filesize
  136. $cdrec .= pack("V",$unc_len); //uncompressed filesize
  137. $cdrec .= pack("v", strlen($name) ); //length of filename
  138. $cdrec .= pack("v", 0 ); //extra field length
  139. $cdrec .= pack("v", 0 ); //file comment length
  140. $cdrec .= pack("v", 0 ); //disk number start
  141. $cdrec .= pack("v", 0 ); //internal file attributes
  142. $cdrec .= pack("V", 32 ); //external file attributes - 'archive' bit set
  143.  
  144. $cdrec .= pack("V", $this -> old_offset ); //relative offset of local header
  145. // echo "old offset is ".$this->old_offset.", new offset is $new_offset<br />";
  146. $this -> old_offset = $new_offset;
  147.  
  148. $cdrec .= $name;
  149. // optional extra field, file comment goes here
  150. // save to central directory
  151. $this -> ctrl_dir[] = $cdrec;
  152.  
  153. }
  154. function file() { // dump out file
  155. $data = implode("", $this -> datasec);
  156. $ctrldir = implode("", $this -> ctrl_dir);
  157. return
  158. $data.
  159. $ctrldir.
  160. $this -> eof_ctrl_dir.
  161. pack("v", sizeof($this -> ctrl_dir)). // total # of entries "on this disk"
  162. pack("v", sizeof($this -> ctrl_dir)). // total # of entries overall
  163. pack("V", strlen($ctrldir)). // size of central dir
  164. pack("V", strlen($data)). // offset to start of central dir
  165. "x00x00"; // .zip file comment length
  166. }
  167. }
  168. ?>

Ejemplo:

PHP:
  1. <?php
  2. $zipfile = new zipfile();
  3.  
  4. // add the subdirectory ... important!
  5. $zipfile -> add_dir("dir/");
  6.  
  7. // add the binary data stored in the string 'filedata'
  8. $filedata = "(read your file into $filedata)";
  9. $zipfile -> add_file($filedata, "dir/file.txt");
  10.  
  11. // the next three lines force an immediate download of the zip file:
  12. header("Content-type: application/octet-stream");
  13. header("Content-disposition: attachment; filename=test.zip");
  14. echo $zipfile -> file();
  15.  
  16.  
  17. // OR instead of doing that, you can write out the file to the loca disk like this:
  18. $filename = "output.zip";
  19. $fd = fopen ($filename, "wb");
  20. $out = fwrite ($fd, $zipfile -> file());
  21. fclose ($fd);
  22.  
  23. ?>

Post to Twitter


  • No Related Post

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Back to Top
[x] Cerrar
E-mail