Просмотр файла inc/zip.class.php

Размер файла: 6.3Kb
  1. <?php
  2.  
  3. /**
  4. * Class to dynamically create a zip file (archive)
  5. *
  6. * @author Rochak Chauhan
  7. */
  8.  
  9. class createZip {
  10.  
  11. public $compressedData = array();
  12. public $centralDirectory = array(); // central directory
  13. public $endOfCentralDirectory = "\x50\x4b\x05\x06\x00\x00\x00\x00"; //end of Central directory record
  14. public $oldOffset = 0;
  15.  
  16. /**
  17. * Function to create the directory where the file(s) will be unzipped
  18. *
  19. * @param $directoryName string
  20. *
  21. */
  22.  
  23. public function addDirectory($directoryName) {
  24. $directoryName = str_replace("\\", "/", $directoryName);
  25.  
  26. $feedArrayRow = "\x50\x4b\x03\x04";
  27. $feedArrayRow .= "\x0a\x00";
  28. $feedArrayRow .= "\x00\x00";
  29. $feedArrayRow .= "\x00\x00";
  30. $feedArrayRow .= "\x00\x00\x00\x00";
  31.  
  32. $feedArrayRow .= pack("V",0);
  33. $feedArrayRow .= pack("V",0);
  34. $feedArrayRow .= pack("V",0);
  35. $feedArrayRow .= pack("v", strlen($directoryName) );
  36. $feedArrayRow .= pack("v", 0 );
  37. $feedArrayRow .= $directoryName;
  38.  
  39. $feedArrayRow .= pack("V",0);
  40. $feedArrayRow .= pack("V",0);
  41. $feedArrayRow .= pack("V",0);
  42.  
  43. $this -> compressedData[] = $feedArrayRow;
  44.  
  45. $newOffset = strlen(implode("", $this->compressedData));
  46.  
  47. $addCentralRecord = "\x50\x4b\x01\x02";
  48. $addCentralRecord .="\x00\x00";
  49. $addCentralRecord .="\x0a\x00";
  50. $addCentralRecord .="\x00\x00";
  51. $addCentralRecord .="\x00\x00";
  52. $addCentralRecord .="\x00\x00\x00\x00";
  53. $addCentralRecord .= pack("V",0);
  54. $addCentralRecord .= pack("V",0);
  55. $addCentralRecord .= pack("V",0);
  56. $addCentralRecord .= pack("v", strlen($directoryName) );
  57. $addCentralRecord .= pack("v", 0 );
  58. $addCentralRecord .= pack("v", 0 );
  59. $addCentralRecord .= pack("v", 0 );
  60. $addCentralRecord .= pack("v", 0 );
  61. $ext = "\x00\x00\x10\x00";
  62. $ext = "\xff\xff\xff\xff";
  63. $addCentralRecord .= pack("V", 16 );
  64.  
  65. $addCentralRecord .= pack("V", $this -> oldOffset );
  66. $this -> oldOffset = $newOffset;
  67.  
  68. $addCentralRecord .= $directoryName;
  69.  
  70. $this -> centralDirectory[] = $addCentralRecord;
  71. }
  72.  
  73. /**
  74. * Function to add file(s) to the specified directory in the archive
  75. *
  76. * @param $directoryName string
  77. *
  78. */
  79.  
  80. public function addFile($data, $directoryName) {
  81.  
  82. $directoryName = str_replace("\\", "/", $directoryName);
  83.  
  84. $feedArrayRow = "\x50\x4b\x03\x04";
  85. $feedArrayRow .= "\x14\x00";
  86. $feedArrayRow .= "\x00\x00";
  87. $feedArrayRow .= "\x08\x00";
  88. $feedArrayRow .= "\x00\x00\x00\x00";
  89.  
  90. $uncompressedLength = strlen($data);
  91. $compression = crc32($data);
  92. $gzCompressedData = gzcompress($data);
  93. $gzCompressedData = substr( substr($gzCompressedData, 0, strlen($gzCompressedData) - 4), 2);
  94. $compressedLength = strlen($gzCompressedData);
  95. $feedArrayRow .= pack("V",$compression);
  96. $feedArrayRow .= pack("V",$compressedLength);
  97. $feedArrayRow .= pack("V",$uncompressedLength);
  98. $feedArrayRow .= pack("v", strlen($directoryName) );
  99. $feedArrayRow .= pack("v", 0 );
  100. $feedArrayRow .= $directoryName;
  101.  
  102. $feedArrayRow .= $gzCompressedData;
  103.  
  104. $feedArrayRow .= pack("V",$compression);
  105. $feedArrayRow .= pack("V",$compressedLength);
  106. $feedArrayRow .= pack("V",$uncompressedLength);
  107.  
  108. $this -> compressedData[] = $feedArrayRow;
  109.  
  110. $newOffset = strlen(implode("", $this->compressedData));
  111.  
  112. $addCentralRecord = "\x50\x4b\x01\x02";
  113. $addCentralRecord .="\x00\x00";
  114. $addCentralRecord .="\x14\x00";
  115. $addCentralRecord .="\x00\x00";
  116. $addCentralRecord .="\x08\x00";
  117. $addCentralRecord .="\x00\x00\x00\x00";
  118. $addCentralRecord .= pack("V",$compression);
  119. $addCentralRecord .= pack("V",$compressedLength);
  120. $addCentralRecord .= pack("V",$uncompressedLength);
  121. $addCentralRecord .= pack("v", strlen($directoryName) );
  122. $addCentralRecord .= pack("v", 0 );
  123. $addCentralRecord .= pack("v", 0 );
  124. $addCentralRecord .= pack("v", 0 );
  125. $addCentralRecord .= pack("v", 0 );
  126. $addCentralRecord .= pack("V", 32 );
  127.  
  128. $addCentralRecord .= pack("V", $this -> oldOffset );
  129. $this -> oldOffset = $newOffset;
  130.  
  131. $addCentralRecord .= $directoryName;
  132.  
  133. $this -> centralDirectory[] = $addCentralRecord;
  134. }
  135.  
  136. /**
  137. * Fucntion to return the zip file
  138. *
  139. * @return zipfile (archive)
  140. */
  141.  
  142. public function getZippedfile() {
  143.  
  144. $data = implode("", $this -> compressedData);
  145. $controlDirectory = implode("", $this -> centralDirectory);
  146.  
  147. return
  148. $data.
  149. $controlDirectory.
  150. $this -> endOfCentralDirectory.
  151. pack("v", sizeof($this -> centralDirectory)).
  152. pack("v", sizeof($this -> centralDirectory)).
  153. pack("V", strlen($controlDirectory)).
  154. pack("V", strlen($data)).
  155. "\x00\x00";
  156. }
  157.  
  158. /**
  159. *
  160. * Function to force the download of the archive as soon as it is created
  161. *
  162. * @param archiveName string - name of the created archive file
  163. */
  164.  
  165. public function forceDownload($archiveName) {
  166. $headerInfo = '';
  167.  
  168. if(ini_get('zlib.output_compression')) {
  169. ini_set('zlib.output_compression', 'Off');
  170. }
  171.  
  172. // Security checks
  173. if( $archiveName == "" ) {
  174. echo "<html><title>Public Photo Directory - Download </title><body><BR><B>ERROR:</B> The download file was NOT SPECIFIED.</body></html>";
  175. exit;
  176. }
  177. elseif ( ! file_exists( $archiveName ) ) {
  178. echo "<html><title>Public Photo Directory - Download </title><body><BR><B>ERROR:</B> File not found.</body></html>";
  179. exit;
  180. }
  181.  
  182. header("Pragma: public");
  183. header("Expires: 0");
  184. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  185. header("Cache-Control: private",false);
  186. header("Content-Type: application/zip");
  187. header("Content-Disposition: attachment; filename=".basename($archiveName).";" );
  188. header("Content-Transfer-Encoding: binary");
  189. header("Content-Length: ".filesize($archiveName));
  190. readfile("$archiveName");
  191.  
  192. }
  193.  
  194. }
  195. ?>