Gz.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Helper class that simplifies gz files stream reading and writing
  8. */
  9. namespace Magento\Framework\Archive\Helper\File;
  10. class Gz extends \Magento\Framework\Archive\Helper\File
  11. {
  12. /**
  13. * {@inheritdoc}
  14. * @throws \RuntimeException
  15. */
  16. protected function _open($mode)
  17. {
  18. if (!extension_loaded('zlib')) {
  19. throw new \RuntimeException('PHP extension zlib is required.');
  20. }
  21. $this->_fileHandler = gzopen($this->_filePath, $mode);
  22. if (false === $this->_fileHandler) {
  23. throw new \Magento\Framework\Exception\LocalizedException(
  24. new \Magento\Framework\Phrase('The "%1" file failed to open.', [$this->_filePath])
  25. );
  26. }
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. protected function _write($data)
  32. {
  33. $result = gzwrite($this->_fileHandler, $data);
  34. if (empty($result) && !empty($data)) {
  35. throw new \Magento\Framework\Exception\LocalizedException(
  36. new \Magento\Framework\Phrase('The data failed to write to "%1".', [$this->_filePath])
  37. );
  38. }
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. protected function _read($length)
  44. {
  45. return gzread($this->_fileHandler, $length);
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. protected function _eof()
  51. {
  52. return gzeof($this->_fileHandler);
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. protected function _close()
  58. {
  59. gzclose($this->_fileHandler);
  60. }
  61. }