Bz.php 1.7 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 bz2 files stream reading and writing
  8. */
  9. namespace Magento\Framework\Archive\Helper\File;
  10. class Bz extends \Magento\Framework\Archive\Helper\File
  11. {
  12. /**
  13. * {@inheritdoc}
  14. * @throws \RuntimeException
  15. */
  16. protected function _open($mode)
  17. {
  18. if (!extension_loaded('bz2')) {
  19. throw new \RuntimeException('PHP extension bz2 is required.');
  20. }
  21. $this->_fileHandler = bzopen($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 = bzwrite($this->_fileHandler, $data);
  34. if (false === $result) {
  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. $data = bzread($this->_fileHandler, $length);
  46. if (false === $data) {
  47. throw new \Magento\Framework\Exception\LocalizedException(
  48. new \Magento\Framework\Phrase('Failed to read data from %1', [$this->_filePath])
  49. );
  50. }
  51. return $data;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. protected function _close()
  57. {
  58. bzclose($this->_fileHandler);
  59. }
  60. }