Source of file Zip.php
Size: 9,419 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Filter/Compress/Zip.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Filter\Compress; use Zend\Filter\Exception; use ZipArchive; /** * Compression adapter for zip */ class Zip extends AbstractCompressionAlgorithm { /** * Compression Options * array( * 'archive' => Archive to use * 'password' => Password to use * 'target' => Target to write the files to * ) * * @var array */ protected $options = array( 'archive' => null, 'target' => null, ); /** * Class constructor * * @param null|array|\Traversable $options (Optional) Options to set * @throws Exception\ExtensionNotLoadedException if zip extension not loaded */ public function __construct($options = null) { if (!extension_loaded('zip')) { throw new Exception\ExtensionNotLoadedException('This filter needs the zip extension'); } parent::__construct($options); } /** * Returns the set archive * * @return string */ public function getArchive() { return $this->options['archive']; } /** * Sets the archive to use for de-/compression * * @param string $archive Archive to use * @return self */ public function setArchive($archive) { $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, (string) $archive); $this->options['archive'] = $archive; return $this; } /** * Returns the set targetpath * * @return string */ public function getTarget() { return $this->options['target']; } /** * Sets the target to use * * @param string $target * @throws Exception\InvalidArgumentException * @return self */ public function setTarget($target) { if (!file_exists(dirname($target))) { throw new Exception\InvalidArgumentException("The directory '$target' does not exist"); } $target = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, (string) $target); $this->options['target'] = $target; return $this; } /** * Compresses the given content * * @param string $content * @return string Compressed archive * @throws Exception\RuntimeException if unable to open zip archive, or error during compression */ public function compress($content) { $zip = new ZipArchive(); $res = $zip->open($this->getArchive(), ZipArchive::CREATE | ZipArchive::OVERWRITE); if ($res !== true) { throw new Exception\RuntimeException($this->errorString($res)); } if (file_exists($content)) { $content = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, realpath($content)); $basename = substr($content, strrpos($content, DIRECTORY_SEPARATOR) + 1); if (is_dir($content)) { $index = strrpos($content, DIRECTORY_SEPARATOR) + 1; $content .= DIRECTORY_SEPARATOR; $stack = array($content); while (!empty($stack)) { $current = array_pop($stack); $files = array(); $dir = dir($current); while (false !== ($node = $dir->read())) { if (($node == '.') || ($node == '..')) { continue; } if (is_dir($current . $node)) { array_push($stack, $current . $node . DIRECTORY_SEPARATOR); } if (is_file($current . $node)) { $files[] = $node; } } $local = substr($current, $index); $zip->addEmptyDir(substr($local, 0, -1)); foreach ($files as $file) { $zip->addFile($current . $file, $local . $file); if ($res !== true) { throw new Exception\RuntimeException($this->errorString($res)); } } } } else { $res = $zip->addFile($content, $basename); if ($res !== true) { throw new Exception\RuntimeException($this->errorString($res)); } } } else { $file = $this->getTarget(); if (!is_dir($file)) { $file = basename($file); } else { $file = "zip.tmp"; } $res = $zip->addFromString($file, $content); if ($res !== true) { throw new Exception\RuntimeException($this->errorString($res)); } } $zip->close(); return $this->options['archive']; } /** * Decompresses the given content * * @param string $content * @return string * @throws Exception\RuntimeException If archive file not found, target directory not found, * or error during decompression */ public function decompress($content) { $archive = $this->getArchive(); if (empty($archive) || !file_exists($archive)) { throw new Exception\RuntimeException('ZIP Archive not found'); } $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, realpath($content)); $zip = new ZipArchive(); $res = $zip->open($archive); $target = $this->getTarget(); if (!empty($target) && !is_dir($target)) { $target = dirname($target); } if (!empty($target)) { $target = rtrim($target, '/\\') . DIRECTORY_SEPARATOR; } if (empty($target) || !is_dir($target)) { throw new Exception\RuntimeException('No target for ZIP decompression set'); } if ($res !== true) { throw new Exception\RuntimeException($this->errorString($res)); } $res = $zip->extractTo($target); if ($res !== true) { throw new Exception\RuntimeException($this->errorString($res)); } $zip->close(); return $target; } /** * Returns the proper string based on the given error constant * * @param string $error * @return string */ public function errorString($error) { switch ($error) { case ZipArchive::ER_MULTIDISK : return 'Multidisk ZIP Archives not supported'; case ZipArchive::ER_RENAME : return 'Failed to rename the temporary file for ZIP'; case ZipArchive::ER_CLOSE : return 'Failed to close the ZIP Archive'; case ZipArchive::ER_SEEK : return 'Failure while seeking the ZIP Archive'; case ZipArchive::ER_READ : return 'Failure while reading the ZIP Archive'; case ZipArchive::ER_WRITE : return 'Failure while writing the ZIP Archive'; case ZipArchive::ER_CRC : return 'CRC failure within the ZIP Archive'; case ZipArchive::ER_ZIPCLOSED : return 'ZIP Archive already closed'; case ZipArchive::ER_NOENT : return 'No such file within the ZIP Archive'; case ZipArchive::ER_EXISTS : return 'ZIP Archive already exists'; case ZipArchive::ER_OPEN : return 'Can not open ZIP Archive'; case ZipArchive::ER_TMPOPEN : return 'Failure creating temporary ZIP Archive'; case ZipArchive::ER_ZLIB : return 'ZLib Problem'; case ZipArchive::ER_MEMORY : return 'Memory allocation problem while working on a ZIP Archive'; case ZipArchive::ER_CHANGED : return 'ZIP Entry has been changed'; case ZipArchive::ER_COMPNOTSUPP : return 'Compression method not supported within ZLib'; case ZipArchive::ER_EOF : return 'Premature EOF within ZIP Archive'; case ZipArchive::ER_INVAL : return 'Invalid argument for ZLIB'; case ZipArchive::ER_NOZIP : return 'Given file is no zip archive'; case ZipArchive::ER_INTERNAL : return 'Internal error while working on a ZIP Archive'; case ZipArchive::ER_INCONS : return 'Inconsistent ZIP archive'; case ZipArchive::ER_REMOVE : return 'Can not remove ZIP Archive'; case ZipArchive::ER_DELETED : return 'ZIP Entry has been deleted'; default : return 'Unknown error within ZIP Archive'; } } /** * Returns the adapter name * * @return string */ public function toString() { return 'Zip'; } } |