ZipStream.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. 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. const FILE_HEADER_SIGNATURE = 0x04034b50;
  87. const CDR_FILE_SIGNATURE = 0x02014b50;
  88. const CDR_EOF_SIGNATURE = 0x06054b50;
  89. const DATA_DESCRIPTOR_SIGNATURE = 0x08074b50;
  90. const ZIP64_CDR_EOF_SIGNATURE = 0x06064b50;
  91. 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. * // create a temporary file stream and write text to it
  296. * $fp = tmpfile();
  297. * fwrite($fp, 'The quick brown fox jumped over the lazy dog.');
  298. *
  299. * // add a file named 'streamfile.txt' from the content of the stream
  300. * $x->addFileFromPsr7Stream('streamfile.txt', $fp);
  301. *
  302. * @return void
  303. */
  304. public function addFileFromPsr7Stream(
  305. string $name,
  306. StreamInterface $stream,
  307. ?FileOptions $options = null
  308. ): void {
  309. $options = $options ?: new FileOptions();
  310. $options->defaultTo($this->opt);
  311. $file = new File($this, $name, $options);
  312. $file->processStream($stream);
  313. }
  314. /**
  315. * finish
  316. *
  317. * Write zip footer to stream.
  318. *
  319. * Example:
  320. *
  321. * // add a list of files to the archive
  322. * $files = array('foo.txt', 'bar.jpg');
  323. * foreach ($files as $path)
  324. * $zip->addFile($path, file_get_contents($path));
  325. *
  326. * // write footer to stream
  327. * $zip->finish();
  328. * @return void
  329. *
  330. * @throws OverflowException
  331. */
  332. public function finish(): void
  333. {
  334. // add trailing cdr file records
  335. foreach ($this->files as $cdrFile) {
  336. $this->send($cdrFile);
  337. $this->cdr_ofs = $this->cdr_ofs->add(Bigint::init(strlen($cdrFile)));
  338. }
  339. // Add 64bit headers (if applicable)
  340. if (count($this->files) >= 0xFFFF ||
  341. $this->cdr_ofs->isOver32() ||
  342. $this->ofs->isOver32()) {
  343. if (!$this->opt->isEnableZip64()) {
  344. throw new OverflowException();
  345. }
  346. $this->addCdr64Eof();
  347. $this->addCdr64Locator();
  348. }
  349. // add trailing cdr eof record
  350. $this->addCdrEof();
  351. // The End
  352. $this->clear();
  353. }
  354. /**
  355. * Send ZIP64 CDR EOF (Central Directory Record End-of-File) record.
  356. *
  357. * @return void
  358. */
  359. protected function addCdr64Eof(): void
  360. {
  361. $num_files = count($this->files);
  362. $cdr_length = $this->cdr_ofs;
  363. $cdr_offset = $this->ofs;
  364. $fields = [
  365. ['V', static::ZIP64_CDR_EOF_SIGNATURE], // ZIP64 end of central file header signature
  366. ['P', 44], // Length of data below this header (length of block - 12) = 44
  367. ['v', static::ZIP_VERSION_MADE_BY], // Made by version
  368. ['v', Version::ZIP64], // Extract by version
  369. ['V', 0x00], // disk number
  370. ['V', 0x00], // no of disks
  371. ['P', $num_files], // no of entries on disk
  372. ['P', $num_files], // no of entries in cdr
  373. ['P', $cdr_length], // CDR size
  374. ['P', $cdr_offset], // CDR offset
  375. ];
  376. $ret = static::packFields($fields);
  377. $this->send($ret);
  378. }
  379. /**
  380. * Create a format string and argument list for pack(), then call
  381. * pack() and return the result.
  382. *
  383. * @param array $fields
  384. * @return string
  385. */
  386. public static function packFields(array $fields): string
  387. {
  388. $fmt = '';
  389. $args = [];
  390. // populate format string and argument list
  391. foreach ($fields as [$format, $value]) {
  392. if ($format === 'P') {
  393. $fmt .= 'VV';
  394. if ($value instanceof Bigint) {
  395. $args[] = $value->getLow32();
  396. $args[] = $value->getHigh32();
  397. } else {
  398. $args[] = $value;
  399. $args[] = 0;
  400. }
  401. } else {
  402. if ($value instanceof Bigint) {
  403. $value = $value->getLow32();
  404. }
  405. $fmt .= $format;
  406. $args[] = $value;
  407. }
  408. }
  409. // prepend format string to argument list
  410. array_unshift($args, $fmt);
  411. // build output string from header and compressed data
  412. return pack(...$args);
  413. }
  414. /**
  415. * Send string, sending HTTP headers if necessary.
  416. * Flush output after write if configure option is set.
  417. *
  418. * @param String $str
  419. * @return void
  420. */
  421. public function send(string $str): void
  422. {
  423. if ($this->need_headers) {
  424. $this->sendHttpHeaders();
  425. }
  426. $this->need_headers = false;
  427. fwrite($this->opt->getOutputStream(), $str);
  428. if ($this->opt->isFlushOutput()) {
  429. // flush output buffer if it is on and flushable
  430. $status = ob_get_status();
  431. if (isset($status['flags']) && ($status['flags'] & PHP_OUTPUT_HANDLER_FLUSHABLE)) {
  432. ob_flush();
  433. }
  434. // Flush system buffers after flushing userspace output buffer
  435. flush();
  436. }
  437. }
  438. /**
  439. * Send HTTP headers for this stream.
  440. *
  441. * @return void
  442. */
  443. protected function sendHttpHeaders(): void
  444. {
  445. // grab content disposition
  446. $disposition = $this->opt->getContentDisposition();
  447. if ($this->output_name) {
  448. // Various different browsers dislike various characters here. Strip them all for safety.
  449. $safe_output = trim(str_replace(['"', "'", '\\', ';', "\n", "\r"], '', $this->output_name));
  450. // Check if we need to UTF-8 encode the filename
  451. $urlencoded = rawurlencode($safe_output);
  452. $disposition .= "; filename*=UTF-8''{$urlencoded}";
  453. }
  454. $headers = array(
  455. 'Content-Type' => $this->opt->getContentType(),
  456. 'Content-Disposition' => $disposition,
  457. 'Pragma' => 'public',
  458. 'Cache-Control' => 'public, must-revalidate',
  459. 'Content-Transfer-Encoding' => 'binary'
  460. );
  461. $call = $this->opt->getHttpHeaderCallback();
  462. foreach ($headers as $key => $val) {
  463. $call("$key: $val");
  464. }
  465. }
  466. /**
  467. * Send ZIP64 CDR Locator (Central Directory Record Locator) record.
  468. *
  469. * @return void
  470. */
  471. protected function addCdr64Locator(): void
  472. {
  473. $cdr_offset = $this->ofs->add($this->cdr_ofs);
  474. $fields = [
  475. ['V', static::ZIP64_CDR_LOCATOR_SIGNATURE], // ZIP64 end of central file header signature
  476. ['V', 0x00], // Disc number containing CDR64EOF
  477. ['P', $cdr_offset], // CDR offset
  478. ['V', 1], // Total number of disks
  479. ];
  480. $ret = static::packFields($fields);
  481. $this->send($ret);
  482. }
  483. /**
  484. * Send CDR EOF (Central Directory Record End-of-File) record.
  485. *
  486. * @return void
  487. */
  488. protected function addCdrEof(): void
  489. {
  490. $num_files = count($this->files);
  491. $cdr_length = $this->cdr_ofs;
  492. $cdr_offset = $this->ofs;
  493. // grab comment (if specified)
  494. $comment = $this->opt->getComment();
  495. $fields = [
  496. ['V', static::CDR_EOF_SIGNATURE], // end of central file header signature
  497. ['v', 0x00], // disk number
  498. ['v', 0x00], // no of disks
  499. ['v', min($num_files, 0xFFFF)], // no of entries on disk
  500. ['v', min($num_files, 0xFFFF)], // no of entries in cdr
  501. ['V', $cdr_length->getLowFF()], // CDR size
  502. ['V', $cdr_offset->getLowFF()], // CDR offset
  503. ['v', strlen($comment)], // Zip Comment size
  504. ];
  505. $ret = static::packFields($fields) . $comment;
  506. $this->send($ret);
  507. }
  508. /**
  509. * Clear all internal variables. Note that the stream object is not
  510. * usable after this.
  511. *
  512. * @return void
  513. */
  514. protected function clear(): void
  515. {
  516. $this->files = [];
  517. $this->ofs = new Bigint();
  518. $this->cdr_ofs = new Bigint();
  519. $this->opt = new ArchiveOptions();
  520. }
  521. /**
  522. * Is this file larger than large_file_size?
  523. *
  524. * @param string $path
  525. * @return bool
  526. */
  527. public function isLargeFile(string $path): bool
  528. {
  529. if (!$this->opt->isStatFiles()) {
  530. return false;
  531. }
  532. $stat = stat($path);
  533. return $stat['size'] > $this->opt->getLargeFileSize();
  534. }
  535. /**
  536. * Save file attributes for trailing CDR record.
  537. *
  538. * @param File $file
  539. * @return void
  540. */
  541. public function addToCdr(File $file): void
  542. {
  543. $file->ofs = $this->ofs;
  544. $this->ofs = $this->ofs->add($file->getTotalLength());
  545. $this->files[] = $file->getCdrFile();
  546. }
  547. }