Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Bz2.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Filter/Compress/CompressAbstract.php';
26 
36 {
46  protected $_options = array(
47  'blocksize' => 4,
48  'archive' => null,
49  );
50 
56  public function __construct($options = null)
57  {
58  if (!extension_loaded('bz2')) {
59  #require_once 'Zend/Filter/Exception.php';
60  throw new Zend_Filter_Exception('This filter needs the bz2 extension');
61  }
62  parent::__construct($options);
63  }
64 
70  public function getBlocksize()
71  {
72  return $this->_options['blocksize'];
73  }
74 
81  public function setBlocksize($blocksize)
82  {
83  if (($blocksize < 0) || ($blocksize > 9)) {
84  #require_once 'Zend/Filter/Exception.php';
85  throw new Zend_Filter_Exception('Blocksize must be between 0 and 9');
86  }
87 
88  $this->_options['blocksize'] = (int) $blocksize;
89  return $this;
90  }
91 
97  public function getArchive()
98  {
99  return $this->_options['archive'];
100  }
101 
108  public function setArchive($archive)
109  {
110  $this->_options['archive'] = (string) $archive;
111  return $this;
112  }
113 
120  public function compress($content)
121  {
122  $archive = $this->getArchive();
123  if (!empty($archive)) {
124  $file = bzopen($archive, 'w');
125  if (!$file) {
126  #require_once 'Zend/Filter/Exception.php';
127  throw new Zend_Filter_Exception("Error opening the archive '" . $archive . "'");
128  }
129 
130  bzwrite($file, $content);
131  bzclose($file);
132  $compressed = true;
133  } else {
134  $compressed = bzcompress($content, $this->getBlocksize());
135  }
136 
137  if (is_int($compressed)) {
138  #require_once 'Zend/Filter/Exception.php';
139  throw new Zend_Filter_Exception('Error during compression');
140  }
141 
142  return $compressed;
143  }
144 
151  public function decompress($content)
152  {
153  $archive = $this->getArchive();
154  if (@file_exists($content)) {
155  $archive = $content;
156  }
157 
158  if (@file_exists($archive)) {
159  $file = bzopen($archive, 'r');
160  if (!$file) {
161  #require_once 'Zend/Filter/Exception.php';
162  throw new Zend_Filter_Exception("Error opening the archive '" . $content . "'");
163  }
164 
165  $compressed = bzread($file);
166  bzclose($file);
167  } else {
168  $compressed = bzdecompress($content);
169  }
170 
171  if (is_int($compressed)) {
172  #require_once 'Zend/Filter/Exception.php';
173  throw new Zend_Filter_Exception('Error during decompression');
174  }
175 
176  return $compressed;
177  }
178 
184  public function toString()
185  {
186  return 'Bz2';
187  }
188 }
__construct($options=null)
Definition: Bz2.php:56
setBlocksize($blocksize)
Definition: Bz2.php:81
compress($content)
Definition: Bz2.php:120
decompress($content)
Definition: Bz2.php:151
setArchive($archive)
Definition: Bz2.php:108