Source for file bzip2.php

Documentation is available at bzip2.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  Archive
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. jimport('joomla.filesystem.file');
  13. jimport('joomla.filesystem.stream');
  14.  
  15. /**
  16.  * Bzip2 format adapter for the JArchive class
  17.  *
  18.  * @package     Joomla.Platform
  19.  * @subpackage  Archive
  20.  * @since       11.1
  21.  */
  22. class JArchiveBzip2 implements JArchiveExtractable
  23. {
  24.     /**
  25.      * Bzip2 file data buffer
  26.      *
  27.      * @var    string 
  28.      * @since  11.1
  29.      */
  30.     private $_data null;
  31.  
  32.     /**
  33.      * Extract a Bzip2 compressed file to a given path
  34.      *
  35.      * @param   string  $archive      Path to Bzip2 archive to extract
  36.      * @param   string  $destination  Path to extract archive to
  37.      * @param   array   $options      Extraction options [unused]
  38.      *
  39.      * @return  boolean  True if successful
  40.      *
  41.      * @since   11.1
  42.      * @throws  RuntimeException
  43.      */
  44.     public function extract($archive$destinationarray $options array ())
  45.     {
  46.         $this->_data null;
  47.  
  48.         if (!extension_loaded('bz2'))
  49.         {
  50.             if (class_exists('JError'))
  51.             {
  52.                 return JError::raiseWarning(100'The bz2 extension is not available.');
  53.             }
  54.             else
  55.             {
  56.                 throw new RuntimeException('The bz2 extension is not available.');
  57.             }
  58.         }
  59.  
  60.         if (!isset($options['use_streams']|| $options['use_streams'== false)
  61.         {
  62.             // Old style: read the whole file and then parse it
  63.             $this->_data file_get_contents($archive);
  64.  
  65.             if (!$this->_data)
  66.             {
  67.                 if (class_exists('JError'))
  68.                 {
  69.                     return JError::raiseWarning(100'Unable to read archive');
  70.                 }
  71.                 else
  72.                 {
  73.                     throw new RuntimeException('Unable to read archive');
  74.                 }
  75.             }
  76.  
  77.             $buffer bzdecompress($this->_data);
  78.             unset($this->_data);
  79.  
  80.             if (empty($buffer))
  81.             {
  82.                 if (class_exists('JError'))
  83.                 {
  84.                     return JError::raiseWarning(100'Unable to decompress data');
  85.                 }
  86.                 else
  87.                 {
  88.                     throw new RuntimeException('Unable to decompress data');
  89.                 }
  90.             }
  91.  
  92.             if (JFile::write($destination$buffer=== false)
  93.             {
  94.                 if (class_exists('JError'))
  95.                 {
  96.                     return JError::raiseWarning(100'Unable to write archive');
  97.                 }
  98.                 else
  99.                 {
  100.                     throw new RuntimeException('Unable to write archive');
  101.                 }
  102.             }
  103.  
  104.         }
  105.         else
  106.         {
  107.             // New style! streams!
  108.             $input JFactory::getStream();
  109.  
  110.             // Use bzip
  111.             $input->set('processingmethod''bz');
  112.  
  113.             if (!$input->open($archive))
  114.             {
  115.                 if (class_exists('JError'))
  116.                 {
  117.                     return JError::raiseWarning(100'Unable to read archive (bz2)');
  118.                 }
  119.                 else
  120.                 {
  121.                     throw new RuntimeException('Unable to read archive (bz2)');
  122.                 }
  123.             }
  124.  
  125.             $output JFactory::getStream();
  126.  
  127.             if (!$output->open($destination'w'))
  128.             {
  129.                 $input->close();
  130.  
  131.                 if (class_exists('JError'))
  132.                 {
  133.                     return JError::raiseWarning(100'Unable to write archive (bz2)');
  134.                 }
  135.                 else
  136.                 {
  137.                     throw new RuntimeException('Unable to write archive (bz2)');
  138.                 }
  139.             }
  140.  
  141.             do
  142.             {
  143.                 $this->_data $input->read($input->get('chunksize'8196));
  144.  
  145.                 if ($this->_data)
  146.                 {
  147.                     if (!$output->write($this->_data))
  148.                     {
  149.                         $input->close();
  150.  
  151.                         if (class_exists('JError'))
  152.                         {
  153.                             return JError::raiseWarning(100'Unable to write archive (bz2)');
  154.                         }
  155.                         else
  156.                         {
  157.                             throw new RuntimeException('Unable to write archive (bz2)');
  158.                         }
  159.                     }
  160.                 }
  161.             }
  162.             while ($this->_data);
  163.  
  164.             $output->close();
  165.             $input->close();
  166.         }
  167.  
  168.         return true;
  169.     }
  170.  
  171.     /**
  172.      * Tests whether this adapter can unpack files on this computer.
  173.      *
  174.      * @return  boolean  True if supported
  175.      *
  176.      * @since   11.3
  177.      */
  178.     public static function isSupported()
  179.     {
  180.         return extension_loaded('bz2');
  181.     }
  182. }

Documentation generated on Tue, 19 Nov 2013 14:54:49 +0100 by phpDocumentor 1.4.3