Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Zend_Filter_Compress_Zip Class Reference
Inheritance diagram for Zend_Filter_Compress_Zip:
Zend_Filter_Compress_CompressAbstract Zend_Filter_Compress_CompressInterface

Public Member Functions

 __construct ($options=null)
 
 getArchive ()
 
 setArchive ($archive)
 
 getTarget ()
 
 setTarget ($target)
 
 compress ($content)
 
 decompress ($content)
 
 toString ()
 
- Public Member Functions inherited from Zend_Filter_Compress_CompressAbstract
 __construct ($options=null)
 
 getOptions ($option=null)
 
 setOptions (array $options)
 

Protected Member Functions

 _errorString ($error)
 

Protected Attributes

 $_options
 

Detailed Description

Definition at line 35 of file Zip.php.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $options = null)

Class constructor

Parameters
string | array$options(Optional) Options to set

Definition at line 57 of file Zip.php.

58  {
59  if (!extension_loaded('zip')) {
60  #require_once 'Zend/Filter/Exception.php';
61  throw new Zend_Filter_Exception('This filter needs the zip extension');
62  }
63  parent::__construct($options);
64  }

Member Function Documentation

◆ _errorString()

_errorString (   $error)
protected

Returns the proper string based on the given error constant

Parameters
string$error

Definition at line 269 of file Zip.php.

270  {
271  switch($error) {
272  case ZipArchive::ER_MULTIDISK :
273  return 'Multidisk ZIP Archives not supported';
274 
275  case ZipArchive::ER_RENAME :
276  return 'Failed to rename the temporary file for ZIP';
277 
278  case ZipArchive::ER_CLOSE :
279  return 'Failed to close the ZIP Archive';
280 
281  case ZipArchive::ER_SEEK :
282  return 'Failure while seeking the ZIP Archive';
283 
284  case ZipArchive::ER_READ :
285  return 'Failure while reading the ZIP Archive';
286 
287  case ZipArchive::ER_WRITE :
288  return 'Failure while writing the ZIP Archive';
289 
290  case ZipArchive::ER_CRC :
291  return 'CRC failure within the ZIP Archive';
292 
293  case ZipArchive::ER_ZIPCLOSED :
294  return 'ZIP Archive already closed';
295 
296  case ZipArchive::ER_NOENT :
297  return 'No such file within the ZIP Archive';
298 
299  case ZipArchive::ER_EXISTS :
300  return 'ZIP Archive already exists';
301 
302  case ZipArchive::ER_OPEN :
303  return 'Can not open ZIP Archive';
304 
305  case ZipArchive::ER_TMPOPEN :
306  return 'Failure creating temporary ZIP Archive';
307 
308  case ZipArchive::ER_ZLIB :
309  return 'ZLib Problem';
310 
311  case ZipArchive::ER_MEMORY :
312  return 'Memory allocation problem while working on a ZIP Archive';
313 
314  case ZipArchive::ER_CHANGED :
315  return 'ZIP Entry has been changed';
316 
317  case ZipArchive::ER_COMPNOTSUPP :
318  return 'Compression method not supported within ZLib';
319 
320  case ZipArchive::ER_EOF :
321  return 'Premature EOF within ZIP Archive';
322 
323  case ZipArchive::ER_INVAL :
324  return 'Invalid argument for ZLIB';
325 
326  case ZipArchive::ER_NOZIP :
327  return 'Given file is no zip archive';
328 
329  case ZipArchive::ER_INTERNAL :
330  return 'Internal error while working on a ZIP Archive';
331 
332  case ZipArchive::ER_INCONS :
333  return 'Inconsistent ZIP archive';
334 
335  case ZipArchive::ER_REMOVE :
336  return 'Can not remove ZIP Archive';
337 
338  case ZipArchive::ER_DELETED :
339  return 'ZIP Entry has been deleted';
340 
341  default :
342  return 'Unknown error within ZIP Archive';
343  }
344  }

◆ compress()

compress (   $content)

Compresses the given content

Parameters
string$content
Returns
string Compressed archive

Implements Zend_Filter_Compress_CompressInterface.

Definition at line 124 of file Zip.php.

125  {
126  $zip = new ZipArchive();
127  $res = $zip->open($this->getArchive(), ZipArchive::CREATE | ZipArchive::OVERWRITE);
128 
129  if ($res !== true) {
130  #require_once 'Zend/Filter/Exception.php';
131  throw new Zend_Filter_Exception($this->_errorString($res));
132  }
133 
134  if (file_exists($content)) {
135  $content = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, realpath($content));
136  $basename = substr($content, strrpos($content, DIRECTORY_SEPARATOR) + 1);
137  if (is_dir($content)) {
138  $index = strrpos($content, DIRECTORY_SEPARATOR) + 1;
139  $content .= DIRECTORY_SEPARATOR;
140  $stack = array($content);
141  while (!empty($stack)) {
142  $current = array_pop($stack);
143  $files = array();
144 
145  $dir = dir($current);
146  while (false !== ($node = $dir->read())) {
147  if (($node == '.') || ($node == '..')) {
148  continue;
149  }
150 
151  if (is_dir($current . $node)) {
152  array_push($stack, $current . $node . DIRECTORY_SEPARATOR);
153  }
154 
155  if (is_file($current . $node)) {
156  $files[] = $node;
157  }
158  }
159 
160  $local = substr($current, $index);
161  $zip->addEmptyDir(substr($local, 0, -1));
162 
163  foreach ($files as $file) {
164  $zip->addFile($current . $file, $local . $file);
165  if ($res !== true) {
166  #require_once 'Zend/Filter/Exception.php';
167  throw new Zend_Filter_Exception($this->_errorString($res));
168  }
169  }
170  }
171  } else {
172  $res = $zip->addFile($content, $basename);
173  if ($res !== true) {
174  #require_once 'Zend/Filter/Exception.php';
175  throw new Zend_Filter_Exception($this->_errorString($res));
176  }
177  }
178  } else {
179  $file = $this->getTarget();
180  if (!is_dir($file)) {
181  $file = basename($file);
182  } else {
183  $file = "zip.tmp";
184  }
185 
186  $res = $zip->addFromString($file, $content);
187  if ($res !== true) {
188  #require_once 'Zend/Filter/Exception.php';
189  throw new Zend_Filter_Exception($this->_errorString($res));
190  }
191  }
192 
193  $zip->close();
194  return $this->_options['archive'];
195  }
_errorString($error)
Definition: Zip.php:269
$index
Definition: list.phtml:44
foreach($appDirs as $dir) $files

◆ decompress()

decompress (   $content)

Decompresses the given content

Parameters
string$content
Returns
string

Implements Zend_Filter_Compress_CompressInterface.

Definition at line 203 of file Zip.php.

204  {
205  $archive = $this->getArchive();
206  if (file_exists($content)) {
207  $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, realpath($content));
208  } elseif (empty($archive) || !file_exists($archive)) {
209  #require_once 'Zend/Filter/Exception.php';
210  throw new Zend_Filter_Exception('ZIP Archive not found');
211  }
212 
213  $zip = new ZipArchive();
214  $res = $zip->open($archive);
215 
216  $target = $this->getTarget();
217 
218  if (!empty($target) && !is_dir($target)) {
219  $target = dirname($target);
220  }
221 
222  if (!empty($target)) {
223  $target = rtrim($target, '/\\') . DIRECTORY_SEPARATOR;
224  }
225 
226  if (empty($target) || !is_dir($target)) {
227  #require_once 'Zend/Filter/Exception.php';
228  throw new Zend_Filter_Exception('No target for ZIP decompression set');
229  }
230 
231  if ($res !== true) {
232  #require_once 'Zend/Filter/Exception.php';
233  throw new Zend_Filter_Exception($this->_errorString($res));
234  }
235 
236  if (version_compare(PHP_VERSION, '5.2.8', '<')) {
237  for ($i = 0; $i < $zip->numFiles; $i++) {
238  $statIndex = $zip->statIndex($i);
239  $currName = $statIndex['name'];
240  if (($currName{0} == '/') ||
241  (substr($currName, 0, 2) == '..') ||
242  (substr($currName, 0, 4) == './..')
243  )
244  {
245  #require_once 'Zend/Filter/Exception.php';
246  throw new Zend_Filter_Exception('Upward directory traversal was detected inside ' . $archive
247  . ' please use PHP 5.2.8 or greater to take advantage of path resolution features of '
248  . 'the zip extension in this decompress() method.'
249  );
250  }
251  }
252  }
253 
254  $res = @$zip->extractTo($target);
255  if ($res !== true) {
256  #require_once 'Zend/Filter/Exception.php';
257  throw new Zend_Filter_Exception($this->_errorString($res));
258  }
259 
260  $zip->close();
261  return $target;
262  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$target
Definition: skip.phtml:8
_errorString($error)
Definition: Zip.php:269
$i
Definition: gallery.phtml:31

◆ getArchive()

getArchive ( )

Returns the set archive

Returns
string

Definition at line 71 of file Zip.php.

72  {
73  return $this->_options['archive'];
74  }

◆ getTarget()

getTarget ( )

Returns the set targetpath

Returns
string

Definition at line 95 of file Zip.php.

96  {
97  return $this->_options['target'];
98  }

◆ setArchive()

setArchive (   $archive)

Sets the archive to use for de-/compression

Parameters
string$archiveArchive to use
Returns
Zend_Filter_Compress_Rar

Definition at line 82 of file Zip.php.

83  {
84  $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $archive);
85  $this->_options['archive'] = (string) $archive;
86 
87  return $this;
88  }

◆ setTarget()

setTarget (   $target)

Sets the target to use

Parameters
string$target
Returns
Zend_Filter_Compress_Rar

Definition at line 106 of file Zip.php.

107  {
108  if (!file_exists(dirname($target))) {
109  #require_once 'Zend/Filter/Exception.php';
110  throw new Zend_Filter_Exception("The directory '$target' does not exist");
111  }
112 
113  $target = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $target);
114  $this->_options['target'] = (string) $target;
115  return $this;
116  }
$target
Definition: skip.phtml:8

◆ toString()

toString ( )

Returns the adapter name

Returns
string

Implements Zend_Filter_Compress_CompressInterface.

Definition at line 351 of file Zip.php.

352  {
353  return 'Zip';
354  }

Field Documentation

◆ $_options

$_options
protected
Initial value:
= array(
'archive' => null,
'target' => null,
)

Definition at line 47 of file Zip.php.


The documentation for this class was generated from the following file: