Archive.php 6.0 KB

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