DeflateStream.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. declare(strict_types=1);
  3. namespace ZipStream;
  4. class DeflateStream extends Stream
  5. {
  6. protected $filter;
  7. /**
  8. * @var Option\File
  9. */
  10. protected $options;
  11. /**
  12. * Rewind stream
  13. *
  14. * @return void
  15. */
  16. public function rewind(): void
  17. {
  18. // deflate filter needs to be removed before rewind
  19. if ($this->filter) {
  20. $this->removeDeflateFilter();
  21. $this->seek(0);
  22. $this->addDeflateFilter($this->options);
  23. } else {
  24. rewind($this->stream);
  25. }
  26. }
  27. /**
  28. * Remove the deflate filter
  29. *
  30. * @return void
  31. */
  32. public function removeDeflateFilter(): void
  33. {
  34. if (!$this->filter) {
  35. return;
  36. }
  37. stream_filter_remove($this->filter);
  38. $this->filter = null;
  39. }
  40. /**
  41. * Add a deflate filter
  42. *
  43. * @param Option\File $options
  44. * @return void
  45. */
  46. public function addDeflateFilter(Option\File $options): void
  47. {
  48. $this->options = $options;
  49. // parameter 4 for stream_filter_append expects array
  50. // so we convert the option object in an array
  51. $optionsArr = [
  52. 'comment' => $options->getComment(),
  53. 'method' => $options->getMethod(),
  54. 'deflateLevel' => $options->getDeflateLevel(),
  55. 'time' => $options->getTime()
  56. ];
  57. $this->filter = stream_filter_append(
  58. $this->stream,
  59. 'zlib.deflate',
  60. STREAM_FILTER_READ,
  61. $optionsArr
  62. );
  63. }
  64. }