Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 400 and 402] [Versions 401 and 402]

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