Archive.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. declare(strict_types=1);
  3. namespace ZipStream\Option;
  4. final class Archive
  5. {
  6. const DEFAULT_DEFLATE_LEVEL = 6;
  7. /**
  8. * @var string
  9. */
  10. private $comment = '';
  11. /**
  12. * Size, in bytes, of the largest file to try
  13. * and load into memory (used by
  14. * addFileFromPath()). Large files may also
  15. * be compressed differently; see the
  16. * 'largeFileMethod' option. Default is ~20 Mb.
  17. *
  18. * @var int
  19. */
  20. private $largeFileSize = 20 * 1024 * 1024;
  21. /**
  22. * How to handle large files. Legal values are
  23. * Method::STORE() (the default), or
  24. * Method::DEFLATE(). STORE sends the file
  25. * raw and is significantly
  26. * faster, while DEFLATE compresses the file
  27. * and is much, much slower. Note that DEFLATE
  28. * must compress the file twice and is extremely slow.
  29. *
  30. * @var Method
  31. */
  32. private $largeFileMethod;
  33. /**
  34. * Boolean indicating whether or not to send
  35. * the HTTP headers for this file.
  36. *
  37. * @var bool
  38. */
  39. private $sendHttpHeaders = false;
  40. /**
  41. * The method called to send headers
  42. *
  43. * @var Callable
  44. */
  45. private $httpHeaderCallback = 'header';
  46. /**
  47. * Enable Zip64 extension, supporting very large
  48. * archives (any size > 4 GB or file count > 64k)
  49. *
  50. * @var bool
  51. */
  52. private $enableZip64 = true;
  53. /**
  54. * Enable streaming files with single read where
  55. * general purpose bit 3 indicates local file header
  56. * contain zero values in crc and size fields,
  57. * these appear only after file contents
  58. * in data descriptor block.
  59. *
  60. * @var bool
  61. */
  62. private $zeroHeader = false;
  63. /**
  64. * Enable reading file stat for determining file size.
  65. * When a 32-bit system reads file size that is
  66. * over 2 GB, invalid value appears in file size
  67. * due to integer overflow. Should be disabled on
  68. * 32-bit systems with method addFileFromPath
  69. * if any file may exceed 2 GB. In this case file
  70. * will be read in blocks and correct size will be
  71. * determined from content.
  72. *
  73. * @var bool
  74. */
  75. private $statFiles = true;
  76. /**
  77. * Enable flush after every write to output stream.
  78. * @var bool
  79. */
  80. private $flushOutput = false;
  81. /**
  82. * HTTP Content-Disposition. Defaults to
  83. * 'attachment', where
  84. * FILENAME is the specified filename.
  85. *
  86. * Note that this does nothing if you are
  87. * not sending HTTP headers.
  88. *
  89. * @var string
  90. */
  91. private $contentDisposition = 'attachment';
  92. /**
  93. * Note that this does nothing if you are
  94. * not sending HTTP headers.
  95. *
  96. * @var string
  97. */
  98. private $contentType = 'application/x-zip';
  99. /**
  100. * @var int
  101. */
  102. private $deflateLevel = 6;
  103. /**
  104. * @var resource
  105. */
  106. private $outputStream;
  107. /**
  108. * Options constructor.
  109. */
  110. public function __construct()
  111. {
  112. $this->largeFileMethod = Method::STORE();
  113. $this->outputStream = fopen('php://output', 'wb');
  114. }
  115. public function getComment(): string
  116. {
  117. return $this->comment;
  118. }
  119. public function setComment(string $comment): void
  120. {
  121. $this->comment = $comment;
  122. }
  123. public function getLargeFileSize(): int
  124. {
  125. return $this->largeFileSize;
  126. }
  127. public function setLargeFileSize(int $largeFileSize): void
  128. {
  129. $this->largeFileSize = $largeFileSize;
  130. }
  131. public function getLargeFileMethod(): Method
  132. {
  133. return $this->largeFileMethod;
  134. }
  135. public function setLargeFileMethod(Method $largeFileMethod): void
  136. {
  137. $this->largeFileMethod = $largeFileMethod;
  138. }
  139. public function isSendHttpHeaders(): bool
  140. {
  141. return $this->sendHttpHeaders;
  142. }
  143. public function setSendHttpHeaders(bool $sendHttpHeaders): void
  144. {
  145. $this->sendHttpHeaders = $sendHttpHeaders;
  146. }
  147. public function getHttpHeaderCallback(): Callable
  148. {
  149. return $this->httpHeaderCallback;
  150. }
  151. public function setHttpHeaderCallback(Callable $httpHeaderCallback): void
  152. {
  153. $this->httpHeaderCallback = $httpHeaderCallback;
  154. }
  155. public function isEnableZip64(): bool
  156. {
  157. return $this->enableZip64;
  158. }
  159. public function setEnableZip64(bool $enableZip64): void
  160. {
  161. $this->enableZip64 = $enableZip64;
  162. }
  163. public function isZeroHeader(): bool
  164. {
  165. return $this->zeroHeader;
  166. }
  167. public function setZeroHeader(bool $zeroHeader): void
  168. {
  169. $this->zeroHeader = $zeroHeader;
  170. }
  171. public function isFlushOutput(): bool
  172. {
  173. return $this->flushOutput;
  174. }
  175. public function setFlushOutput(bool $flushOutput): void
  176. {
  177. $this->flushOutput = $flushOutput;
  178. }
  179. public function isStatFiles(): bool
  180. {
  181. return $this->statFiles;
  182. }
  183. public function setStatFiles(bool $statFiles): void
  184. {
  185. $this->statFiles = $statFiles;
  186. }
  187. public function getContentDisposition(): string
  188. {
  189. return $this->contentDisposition;
  190. }
  191. public function setContentDisposition(string $contentDisposition): void
  192. {
  193. $this->contentDisposition = $contentDisposition;
  194. }
  195. public function getContentType(): string
  196. {
  197. return $this->contentType;
  198. }
  199. public function setContentType(string $contentType): void
  200. {
  201. $this->contentType = $contentType;
  202. }
  203. /**
  204. * @return resource
  205. */
  206. public function getOutputStream()
  207. {
  208. return $this->outputStream;
  209. }
  210. /**
  211. * @param resource $outputStream
  212. */
  213. public function setOutputStream($outputStream): void
  214. {
  215. $this->outputStream = $outputStream;
  216. }
  217. /**
  218. * @return int
  219. */
  220. public function getDeflateLevel(): int
  221. {
  222. return $this->deflateLevel;
  223. }
  224. /**
  225. * @param int $deflateLevel
  226. */
  227. public function setDeflateLevel(int $deflateLevel): void
  228. {
  229. $this->deflateLevel = $deflateLevel;
  230. }
  231. }