File indexing completed on 2025-01-19 05:21:07

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Filter
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Filter_Compress_CompressAbstract
0024  */
0025 // require_once 'Zend/Filter/Compress/CompressAbstract.php';
0026 
0027 /**
0028  * Compression adapter for Bz2
0029  *
0030  * @category   Zend
0031  * @package    Zend_Filter
0032  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0033  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0034  */
0035 class Zend_Filter_Compress_Bz2 extends Zend_Filter_Compress_CompressAbstract
0036 {
0037     /**
0038      * Compression Options
0039      * array(
0040      *     'blocksize' => Blocksize to use from 0-9
0041      *     'archive'   => Archive to use
0042      * )
0043      *
0044      * @var array
0045      */
0046     protected $_options = array(
0047         'blocksize' => 4,
0048         'archive'   => null,
0049     );
0050 
0051     /**
0052      * Class constructor
0053      *
0054      * @param array|Zend_Config $options (Optional) Options to set
0055      */
0056     public function __construct($options = null)
0057     {
0058         if (!extension_loaded('bz2')) {
0059             // require_once 'Zend/Filter/Exception.php';
0060             throw new Zend_Filter_Exception('This filter needs the bz2 extension');
0061         }
0062         parent::__construct($options);
0063     }
0064 
0065     /**
0066      * Returns the set blocksize
0067      *
0068      * @return integer
0069      */
0070     public function getBlocksize()
0071     {
0072         return $this->_options['blocksize'];
0073     }
0074 
0075     /**
0076      * Sets a new blocksize
0077      *
0078      * @param integer $level
0079      * @return Zend_Filter_Compress_Bz2
0080      */
0081     public function setBlocksize($blocksize)
0082     {
0083         if (($blocksize < 0) || ($blocksize > 9)) {
0084             // require_once 'Zend/Filter/Exception.php';
0085             throw new Zend_Filter_Exception('Blocksize must be between 0 and 9');
0086         }
0087 
0088         $this->_options['blocksize'] = (int) $blocksize;
0089         return $this;
0090     }
0091 
0092     /**
0093      * Returns the set archive
0094      *
0095      * @return string
0096      */
0097     public function getArchive()
0098     {
0099         return $this->_options['archive'];
0100     }
0101 
0102     /**
0103      * Sets the archive to use for de-/compression
0104      *
0105      * @param string $archive Archive to use
0106      * @return Zend_Filter_Compress_Bz2
0107      */
0108     public function setArchive($archive)
0109     {
0110         $this->_options['archive'] = (string) $archive;
0111         return $this;
0112     }
0113 
0114     /**
0115      * Compresses the given content
0116      *
0117      * @param  string $content
0118      * @return string
0119      */
0120     public function compress($content)
0121     {
0122         $archive = $this->getArchive();
0123         if (!empty($archive)) {
0124             $file = bzopen($archive, 'w');
0125             if (!$file) {
0126                 // require_once 'Zend/Filter/Exception.php';
0127                 throw new Zend_Filter_Exception("Error opening the archive '" . $archive . "'");
0128             }
0129 
0130             bzwrite($file, $content);
0131             bzclose($file);
0132             $compressed = true;
0133         } else {
0134             $compressed = bzcompress($content, $this->getBlocksize());
0135         }
0136 
0137         if (is_int($compressed)) {
0138             // require_once 'Zend/Filter/Exception.php';
0139             throw new Zend_Filter_Exception('Error during compression');
0140         }
0141 
0142         return $compressed;
0143     }
0144 
0145     /**
0146      * Decompresses the given content
0147      *
0148      * @param  string $content
0149      * @return string
0150      */
0151     public function decompress($content)
0152     {
0153         $archive = $this->getArchive();
0154         if (@file_exists($content)) {
0155             $archive = $content;
0156         }
0157 
0158         if (@file_exists($archive)) {
0159             $file = bzopen($archive, 'r');
0160             if (!$file) {
0161                 // require_once 'Zend/Filter/Exception.php';
0162                 throw new Zend_Filter_Exception("Error opening the archive '" . $content . "'");
0163             }
0164 
0165             $compressed = bzread($file);
0166             bzclose($file);
0167         } else {
0168             $compressed = bzdecompress($content);
0169         }
0170 
0171         if (is_int($compressed)) {
0172             // require_once 'Zend/Filter/Exception.php';
0173             throw new Zend_Filter_Exception('Error during decompression');
0174         }
0175 
0176         return $compressed;
0177     }
0178 
0179     /**
0180      * Returns the adapter name
0181      *
0182      * @return string
0183      */
0184     public function toString()
0185     {
0186         return 'Bz2';
0187     }
0188 }