Source of file Bz2.php
Size: 4,161 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Filter/Compress/Bz2.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 | <?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; /** * Compression adapter for Bz2 */ class Bz2 extends AbstractCompressionAlgorithm { /** * Compression Options * array( * 'blocksize' => Blocksize to use from 0-9 * 'archive' => Archive to use * ) * * @var array */ protected $options = array( 'blocksize' => 4, 'archive' => null, ); /** * Class constructor * * @param null|array|\Traversable $options (Optional) Options to set * @throws Exception\ExtensionNotLoadedException if bz2 extension not loaded */ public function __construct($options = null) { if (!extension_loaded('bz2')) { throw new Exception\ExtensionNotLoadedException('This filter needs the bz2 extension'); } parent::__construct($options); } /** * Returns the set blocksize * * @return int */ public function getBlocksize() { return $this->options['blocksize']; } /** * Sets a new blocksize * * @param int $blocksize * @throws Exception\InvalidArgumentException * @return self */ public function setBlocksize($blocksize) { if (($blocksize < 0) || ($blocksize > 9)) { throw new Exception\InvalidArgumentException('Blocksize must be between 0 and 9'); } $this->options['blocksize'] = (int) $blocksize; return $this; } /** * 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) { $this->options['archive'] = (string) $archive; return $this; } /** * Compresses the given content * * @param string $content * @return string * @throws Exception\RuntimeException */ public function compress($content) { $archive = $this->getArchive(); if (!empty($archive)) { $file = bzopen($archive, 'w'); if (!$file) { throw new Exception\RuntimeException("Error opening the archive '" . $archive . "'"); } bzwrite($file, $content); bzclose($file); $compressed = true; } else { $compressed = bzcompress($content, $this->getBlocksize()); } if (is_int($compressed)) { throw new Exception\RuntimeException('Error during compression'); } return $compressed; } /** * Decompresses the given content * * @param string $content * @return string * @throws Exception\RuntimeException */ public function decompress($content) { $archive = $this->getArchive(); //check if there are null byte characters before doing a file_exists check if (!strstr($content, "\0") && file_exists($content)) { $archive = $content; } if (file_exists($archive)) { $file = bzopen($archive, 'r'); if (!$file) { throw new Exception\RuntimeException("Error opening the archive '" . $content . "'"); } $compressed = bzread($file); bzclose($file); } else { $compressed = bzdecompress($content); } if (is_int($compressed)) { throw new Exception\RuntimeException('Error during decompression'); } return $compressed; } /** * Returns the adapter name * * @return string */ public function toString() { return 'Bz2'; } } |