ZipStream.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. <?php
  2. declare(strict_types=1);
  3. namespace ZipStream;
  4. use Psr\Http\Message\StreamInterface;
  5. use ZipStream\Exception\OverflowException;
  6. use ZipStream\Option\Archive as ArchiveOptions;
  7. use ZipStream\Option\File as FileOptions;
  8. use ZipStream\Option\Version;
  9. /**
  10. * ZipStream
  11. *
  12. * Streamed, dynamically generated zip archives.
  13. *
  14. * Usage:
  15. *
  16. * Streaming zip archives is a simple, three-step process:
  17. *
  18. * 1. Create the zip stream:
  19. *
  20. * $zip = new ZipStream('example.zip');
  21. *
  22. * 2. Add one or more files to the archive:
  23. *
  24. * * add first file
  25. * $data = file_get_contents('some_file.gif');
  26. * $zip->addFile('some_file.gif', $data);
  27. *
  28. * * add second file
  29. * $data = file_get_contents('some_file.gif');
  30. * $zip->addFile('another_file.png', $data);
  31. *
  32. * 3. Finish the zip stream:
  33. *
  34. * $zip->finish();
  35. *
  36. * You can also add an archive comment, add comments to individual files,
  37. * and adjust the timestamp of files. See the API documentation for each
  38. * method below for additional information.
  39. *
  40. * Example:
  41. *
  42. * // create a new zip stream object
  43. * $zip = new ZipStream('some_files.zip');
  44. *
  45. * // list of local files
  46. * $files = array('foo.txt', 'bar.jpg');
  47. *
  48. * // read and add each file to the archive
  49. * foreach ($files as $path)
  50. * $zip->addFile($path, file_get_contents($path));
  51. *
  52. * // write archive footer to stream
  53. * $zip->finish();
  54. */
  55. class ZipStream
  56. {
  57. /**
  58. * This number corresponds to the ZIP version/OS used (2 bytes)
  59. * From: https://www.iana.org/assignments/media-types/application/zip
  60. * The upper byte (leftmost one) indicates the host system (OS) for the
  61. * file. Software can use this information to determine
  62. * the line record format for text files etc. The current
  63. * mappings are:
  64. *
  65. * 0 - MS-DOS and OS/2 (F.A.T. file systems)
  66. * 1 - Amiga 2 - VAX/VMS
  67. * 3 - *nix 4 - VM/CMS
  68. * 5 - Atari ST 6 - OS/2 H.P.F.S.
  69. * 7 - Macintosh 8 - Z-System
  70. * 9 - CP/M 10 thru 255 - unused
  71. *
  72. * The lower byte (rightmost one) indicates the version number of the
  73. * software used to encode the file. The value/10
  74. * indicates the major version number, and the value
  75. * mod 10 is the minor version number.
  76. * Here we are using 6 for the OS, indicating OS/2 H.P.F.S.
  77. * to prevent file permissions issues upon extract (see #84)
  78. * 0x603 is 00000110 00000011 in binary, so 6 and 3
  79. */
  80. public const ZIP_VERSION_MADE_BY = 0x603;
  81. /**
  82. * The following signatures end with 0x4b50, which in ASCII is PK,
  83. * the initials of the inventor Phil Katz.
  84. * See https://en.wikipedia.org/wiki/Zip_(file_format)#File_headers
  85. */
  86. public const FILE_HEADER_SIGNATURE = 0x04034b50;
  87. public const CDR_FILE_SIGNATURE = 0x02014b50;
  88. public const CDR_EOF_SIGNATURE = 0x06054b50;
  89. public const DATA_DESCRIPTOR_SIGNATURE = 0x08074b50;
  90. public const ZIP64_CDR_EOF_SIGNATURE = 0x06064b50;
  91. public const ZIP64_CDR_LOCATOR_SIGNATURE = 0x07064b50;
  92. /**
  93. * Global Options
  94. *
  95. * @var ArchiveOptions
  96. */
  97. public $opt;
  98. /**
  99. * @var array
  100. */
  101. public $files = [];
  102. /**
  103. * @var Bigint
  104. */
  105. public $cdr_ofs;
  106. /**
  107. * @var Bigint
  108. */
  109. public $ofs;
  110. /**
  111. * @var bool
  112. */
  113. protected $need_headers;
  114. /**
  115. * @var null|String
  116. */
  117. protected $output_name;
  118. /**
  119. * Create a new ZipStream object.
  120. *
  121. * Parameters:
  122. *
  123. * @param String $name - Name of output file (optional).
  124. * @param ArchiveOptions $opt - Archive Options
  125. *
  126. * Large File Support:
  127. *
  128. * By default, the method addFileFromPath() will send send files
  129. * larger than 20 megabytes along raw rather than attempting to
  130. * compress them. You can change both the maximum size and the
  131. * compression behavior using the largeFile* options above, with the
  132. * following caveats:
  133. *
  134. * * For "small" files (e.g. files smaller than largeFileSize), the
  135. * memory use can be up to twice that of the actual file. In other
  136. * words, adding a 10 megabyte file to the archive could potentially
  137. * occupy 20 megabytes of memory.
  138. *
  139. * * Enabling compression on large files (e.g. files larger than
  140. * large_file_size) is extremely slow, because ZipStream has to pass
  141. * over the large file once to calculate header information, and then
  142. * again to compress and send the actual data.
  143. *
  144. * Examples:
  145. *
  146. * // create a new zip file named 'foo.zip'
  147. * $zip = new ZipStream('foo.zip');
  148. *
  149. * // create a new zip file named 'bar.zip' with a comment
  150. * $opt->setComment = 'this is a comment for the zip file.';
  151. * $zip = new ZipStream('bar.zip', $opt);
  152. *
  153. * Notes:
  154. *
  155. * In order to let this library send HTTP headers, a filename must be given
  156. * _and_ the option `sendHttpHeaders` must be `true`. This behavior is to
  157. * allow software to send its own headers (including the filename), and
  158. * still use this library.
  159. */
  160. public function __construct(?string $name = null, ?ArchiveOptions $opt = null)
  161. {
  162. $this->opt = $opt ?: new ArchiveOptions();
  163. $this->output_name = $name;
  164. $this->need_headers = $name && $this->opt->isSendHttpHeaders();
  165. $this->cdr_ofs = new Bigint();
  166. $this->ofs = new Bigint();
  167. }
  168. /**
  169. * addFile
  170. *
  171. * Add a file to the archive.
  172. *
  173. * @param String $name - path of file in archive (including directory).
  174. * @param String $data - contents of file
  175. * @param FileOptions $options
  176. *
  177. * File Options:
  178. * time - Last-modified timestamp (seconds since the epoch) of
  179. * this file. Defaults to the current time.
  180. * comment - Comment related to this file.
  181. * method - Storage method for file ("store" or "deflate")
  182. *
  183. * Examples:
  184. *
  185. * // add a file named 'foo.txt'
  186. * $data = file_get_contents('foo.txt');
  187. * $zip->addFile('foo.txt', $data);
  188. *
  189. * // add a file named 'bar.jpg' with a comment and a last-modified
  190. * // time of two hours ago
  191. * $data = file_get_contents('bar.jpg');
  192. * $opt->setTime = time() - 2 * 3600;
  193. * $opt->setComment = 'this is a comment about bar.jpg';
  194. * $zip->addFile('bar.jpg', $data, $opt);
  195. */
  196. public function addFile(string $name, string $data, ?FileOptions $options = null): void
  197. {
  198. $options = $options ?: new FileOptions();
  199. $options->defaultTo($this->opt);
  200. $file = new File($this, $name, $options);
  201. $file->processData($data);
  202. }
  203. /**
  204. * addFileFromPath
  205. *
  206. * Add a file at path to the archive.
  207. *
  208. * Note that large files may be compressed differently than smaller
  209. * files; see the "Large File Support" section above for more
  210. * information.
  211. *
  212. * @param String $name - name of file in archive (including directory path).
  213. * @param String $path - path to file on disk (note: paths should be encoded using
  214. * UNIX-style forward slashes -- e.g '/path/to/some/file').
  215. * @param FileOptions $options
  216. *
  217. * File Options:
  218. * time - Last-modified timestamp (seconds since the epoch) of
  219. * this file. Defaults to the current time.
  220. * comment - Comment related to this file.
  221. * method - Storage method for file ("store" or "deflate")
  222. *
  223. * Examples:
  224. *
  225. * // add a file named 'foo.txt' from the local file '/tmp/foo.txt'
  226. * $zip->addFileFromPath('foo.txt', '/tmp/foo.txt');
  227. *
  228. * // add a file named 'bigfile.rar' from the local file
  229. * // '/usr/share/bigfile.rar' with a comment and a last-modified
  230. * // time of two hours ago
  231. * $path = '/usr/share/bigfile.rar';
  232. * $opt->setTime = time() - 2 * 3600;
  233. * $opt->setComment = 'this is a comment about bar.jpg';
  234. * $zip->addFileFromPath('bigfile.rar', $path, $opt);
  235. *
  236. * @return void
  237. * @throws \ZipStream\Exception\FileNotFoundException
  238. * @throws \ZipStream\Exception\FileNotReadableException
  239. */
  240. public function addFileFromPath(string $name, string $path, ?FileOptions $options = null): void
  241. {
  242. $options = $options ?: new FileOptions();
  243. $options->defaultTo($this->opt);
  244. $file = new File($this, $name, $options);
  245. $file->processPath($path);
  246. }
  247. /**
  248. * addFileFromStream
  249. *
  250. * Add an open stream to the archive.
  251. *
  252. * @param String $name - path of file in archive (including directory).
  253. * @param resource $stream - contents of file as a stream resource
  254. * @param FileOptions $options
  255. *
  256. * File Options:
  257. * time - Last-modified timestamp (seconds since the epoch) of
  258. * this file. Defaults to the current time.
  259. * comment - Comment related to this file.
  260. *
  261. * Examples:
  262. *
  263. * // create a temporary file stream and write text to it
  264. * $fp = tmpfile();
  265. * fwrite($fp, 'The quick brown fox jumped over the lazy dog.');
  266. *
  267. * // add a file named 'streamfile.txt' from the content of the stream
  268. * $x->addFileFromStream('streamfile.txt', $fp);
  269. *
  270. * @return void
  271. */
  272. public function addFileFromStream(string $name, $stream, ?FileOptions $options = null): void
  273. {
  274. $options = $options ?: new FileOptions();
  275. $options->defaultTo($this->opt);
  276. $file = new File($this, $name, $options);
  277. $file->processStream(new DeflateStream($stream));
  278. }
  279. /**
  280. * addFileFromPsr7Stream
  281. *
  282. * Add an open stream to the archive.
  283. *
  284. * @param String $name - path of file in archive (including directory).
  285. * @param StreamInterface $stream - contents of file as a stream resource
  286. * @param FileOptions $options
  287. *
  288. * File Options:
  289. * time - Last-modified timestamp (seconds since the epoch) of
  290. * this file. Defaults to the current time.
  291. * comment - Comment related to this file.
  292. *
  293. * Examples:
  294. *
  295. * $stream = $response->getBody();
  296. * // add a file named 'streamfile.txt' from the content of the stream
  297. * $x->addFileFromPsr7Stream('streamfile.txt', $stream);
  298. *
  299. * @return void
  300. */
  301. public function addFileFromPsr7Stream(
  302. string $name,
  303. StreamInterface $stream,
  304. ?FileOptions $options = null
  305. ): void {
  306. $options = $options ?: new FileOptions();
  307. $options->defaultTo($this->opt);
  308. $file = new File($this, $name, $options);
  309. $file->processStream($stream);
  310. }
  311. /**
  312. * finish
  313. *
  314. * Write zip footer to stream.
  315. *
  316. * Example:
  317. *
  318. * // add a list of files to the archive
  319. * $files = array('foo.txt', 'bar.jpg');
  320. * foreach ($files as $path)
  321. * $zip->addFile($path, file_get_contents($path));
  322. *
  323. * // write footer to stream
  324. * $zip->finish();
  325. * @return void
  326. *
  327. * @throws OverflowException
  328. */
  329. public function finish(): void
  330. {
  331. // add trailing cdr file records
  332. foreach ($this->files as $cdrFile) {
  333. $this->send($cdrFile);
  334. $this->cdr_ofs = $this->cdr_ofs->add(Bigint::init(strlen($cdrFile)));
  335. }
  336. // Add 64bit headers (if applicable)
  337. if (count($this->files) >= 0xFFFF ||
  338. $this->cdr_ofs->isOver32() ||
  339. $this->ofs->isOver32()) {
  340. if (!$this->opt->isEnableZip64()) {
  341. throw new OverflowException();
  342. }
  343. $this->addCdr64Eof();
  344. $this->addCdr64Locator();
  345. }
  346. // add trailing cdr eof record
  347. $this->addCdrEof();
  348. // The End
  349. $this->clear();
  350. }
  351. /**
  352. * Create a format string and argument list for pack(), then call
  353. * pack() and return the result.
  354. *
  355. * @param array $fields
  356. * @return string
  357. */
  358. public static function packFields(array $fields): string
  359. {
  360. $fmt = '';
  361. $args = [];
  362. // populate format string and argument list
  363. foreach ($fields as [$format, $value]) {
  364. if ($format === 'P') {
  365. $fmt .= 'VV';
  366. if ($value instanceof Bigint) {
  367. $args[] = $value->getLow32();
  368. $args[] = $value->getHigh32();
  369. } else {
  370. $args[] = $value;
  371. $args[] = 0;
  372. }
  373. } else {
  374. if ($value instanceof Bigint) {
  375. $value = $value->getLow32();
  376. }
  377. $fmt .= $format;
  378. $args[] = $value;
  379. }
  380. }
  381. // prepend format string to argument list
  382. array_unshift($args, $fmt);
  383. // build output string from header and compressed data
  384. return pack(...$args);
  385. }
  386. /**
  387. * Send string, sending HTTP headers if necessary.
  388. * Flush output after write if configure option is set.
  389. *
  390. * @param String $str
  391. * @return void
  392. */
  393. public function send(string $str): void
  394. {
  395. if ($this->need_headers) {
  396. $this->sendHttpHeaders();
  397. }
  398. $this->need_headers = false;
  399. $outputStream = $this->opt->getOutputStream();
  400. if ($outputStream instanceof StreamInterface) {
  401. $outputStream->write($str);
  402. } else {
  403. fwrite($outputStream, $str);
  404. }
  405. if ($this->opt->isFlushOutput()) {
  406. // flush output buffer if it is on and flushable
  407. $status = ob_get_status();
  408. if (isset($status['flags']) && ($status['flags'] & PHP_OUTPUT_HANDLER_FLUSHABLE)) {
  409. ob_flush();
  410. }
  411. // Flush system buffers after flushing userspace output buffer
  412. flush();
  413. }
  414. }
  415. /**
  416. * Is this file larger than large_file_size?
  417. *
  418. * @param string $path
  419. * @return bool
  420. */
  421. public function isLargeFile(string $path): bool
  422. {
  423. if (!$this->opt->isStatFiles()) {
  424. return false;
  425. }
  426. $stat = stat($path);
  427. return $stat['size'] > $this->opt->getLargeFileSize();
  428. }
  429. /**
  430. * Save file attributes for trailing CDR record.
  431. *
  432. * @param File $file
  433. * @return void
  434. */
  435. public function addToCdr(File $file): void
  436. {
  437. $file->ofs = $this->ofs;
  438. $this->ofs = $this->ofs->add($file->getTotalLength());
  439. $this->files[] = $file->getCdrFile();
  440. }
  441. /**
  442. * Send ZIP64 CDR EOF (Central Directory Record End-of-File) record.
  443. *
  444. * @return void
  445. */
  446. protected function addCdr64Eof(): void
  447. {
  448. $num_files = count($this->files);
  449. $cdr_length = $this->cdr_ofs;
  450. $cdr_offset = $this->ofs;
  451. $fields = [
  452. ['V', static::ZIP64_CDR_EOF_SIGNATURE], // ZIP64 end of central file header signature
  453. ['P', 44], // Length of data below this header (length of block - 12) = 44
  454. ['v', static::ZIP_VERSION_MADE_BY], // Made by version
  455. ['v', Version::ZIP64], // Extract by version
  456. ['V', 0x00], // disk number
  457. ['V', 0x00], // no of disks
  458. ['P', $num_files], // no of entries on disk
  459. ['P', $num_files], // no of entries in cdr
  460. ['P', $cdr_length], // CDR size
  461. ['P', $cdr_offset], // CDR offset
  462. ];
  463. $ret = static::packFields($fields);
  464. $this->send($ret);
  465. }
  466. /**
  467. * Send HTTP headers for this stream.
  468. *
  469. * @return void
  470. */
  471. protected function sendHttpHeaders(): void
  472. {
  473. // grab content disposition
  474. $disposition = $this->opt->getContentDisposition();
  475. if ($this->output_name) {
  476. // Various different browsers dislike various characters here. Strip them all for safety.
  477. $safe_output = trim(str_replace(['"', "'", '\\', ';', "\n", "\r"], '', $this->output_name));
  478. // Check if we need to UTF-8 encode the filename
  479. $urlencoded = rawurlencode($safe_output);
  480. $disposition .= "; filename*=UTF-8''{$urlencoded}";
  481. }
  482. $headers = [
  483. 'Content-Type' => $this->opt->getContentType(),
  484. 'Content-Disposition' => $disposition,
  485. 'Pragma' => 'public',
  486. 'Cache-Control' => 'public, must-revalidate',
  487. 'Content-Transfer-Encoding' => 'binary',
  488. ];
  489. $call = $this->opt->getHttpHeaderCallback();
  490. foreach ($headers as $key => $val) {
  491. $call("$key: $val");
  492. }
  493. }
  494. /**
  495. * Send ZIP64 CDR Locator (Central Directory Record Locator) record.
  496. *
  497. * @return void
  498. */
  499. protected function addCdr64Locator(): void
  500. {
  501. $cdr_offset = $this->ofs->add($this->cdr_ofs);
  502. $fields = [
  503. ['V', static::ZIP64_CDR_LOCATOR_SIGNATURE], // ZIP64 end of central file header signature
  504. ['V', 0x00], // Disc number containing CDR64EOF
  505. ['P', $cdr_offset], // CDR offset
  506. ['V', 1], // Total number of disks
  507. ];
  508. $ret = static::packFields($fields);
  509. $this->send($ret);
  510. }
  511. /**
  512. * Send CDR EOF (Central Directory Record End-of-File) record.
  513. *
  514. * @return void
  515. */
  516. protected function addCdrEof(): void
  517. {
  518. $num_files = count($this->files);
  519. $cdr_length = $this->cdr_ofs;
  520. $cdr_offset = $this->ofs;
  521. // grab comment (if specified)
  522. $comment = $this->opt->getComment();
  523. $fields = [
  524. ['V', static::CDR_EOF_SIGNATURE], // end of central file header signature
  525. ['v', 0x00], // disk number
  526. ['v', 0x00], // no of disks
  527. ['v', min($num_files, 0xFFFF)], // no of entries on disk
  528. ['v', min($num_files, 0xFFFF)], // no of entries in cdr
  529. ['V', $cdr_length->getLowFF()], // CDR size
  530. ['V', $cdr_offset->getLowFF()], // CDR offset
  531. ['v', strlen($comment)], // Zip Comment size
  532. ];
  533. $ret = static::packFields($fields) . $comment;
  534. $this->send($ret);
  535. }
  536. /**
  537. * Clear all internal variables. Note that the stream object is not
  538. * usable after this.
  539. *
  540. * @return void
  541. */
  542. protected function clear(): void
  543. {
  544. $this->files = [];
  545. $this->ofs = new Bigint();
  546. $this->cdr_ofs = new Bigint();
  547. $this->opt = new ArchiveOptions();
  548. }
  549. }