createzip.php 6.5 KB

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