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:

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

Ejemplo:

  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. ?>