[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/joomla/archive/src/ -> Bzip2.php (source)

   1  <?php
   2  /**
   3   * Part of the Joomla Framework Archive Package
   4   *
   5   * @copyright  Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
   6   * @license    GNU General Public License version 2 or later; see LICENSE
   7   */
   8  
   9  namespace Joomla\Archive;
  10  
  11  use Joomla\Filesystem\File;
  12  use Joomla\Filesystem\Stream;
  13  
  14  /**
  15   * Bzip2 format adapter for the Archive package
  16   *
  17   * @since  1.0
  18   */
  19  class Bzip2 implements ExtractableInterface
  20  {
  21      /**
  22       * Bzip2 file data buffer
  23       *
  24       * @var    string
  25       * @since  1.0
  26       */
  27      private $data;
  28  
  29      /**
  30       * Holds the options array.
  31       *
  32       * @var    array|\ArrayAccess
  33       * @since  1.0
  34       */
  35      protected $options = [];
  36  
  37      /**
  38       * Create a new Archive object.
  39       *
  40       * @param   array|\ArrayAccess  $options  An array of options
  41       *
  42       * @since   1.0
  43       * @throws  \InvalidArgumentException
  44       */
  45  	public function __construct($options = [])
  46      {
  47          if (!\is_array($options) && !($options instanceof \ArrayAccess))
  48          {
  49              throw new \InvalidArgumentException(
  50                  'The options param must be an array or implement the ArrayAccess interface.'
  51              );
  52          }
  53  
  54          $this->options = $options;
  55      }
  56  
  57      /**
  58       * Extract a Bzip2 compressed file to a given path
  59       *
  60       * @param   string  $archive      Path to Bzip2 archive to extract
  61       * @param   string  $destination  Path to extract archive to
  62       *
  63       * @return  boolean  True if successful
  64       *
  65       * @since   1.0
  66       * @throws  \RuntimeException
  67       */
  68  	public function extract($archive, $destination)
  69      {
  70          $this->data = null;
  71  
  72          if (!isset($this->options['use_streams']) || $this->options['use_streams'] == false)
  73          {
  74              // Old style: read the whole file and then parse it
  75              $this->data = file_get_contents($archive);
  76  
  77              if (!$this->data)
  78              {
  79                  throw new \RuntimeException('Unable to read archive');
  80              }
  81  
  82              $buffer = bzdecompress($this->data);
  83              unset($this->data);
  84  
  85              if (empty($buffer))
  86              {
  87                  throw new \RuntimeException('Unable to decompress data');
  88              }
  89  
  90              if (!File::write($destination, $buffer))
  91              {
  92                  throw new \RuntimeException('Unable to write archive to file ' . $destination);
  93              }
  94          }
  95          else
  96          {
  97              // New style! streams!
  98              $input = Stream::getStream();
  99  
 100              // Use bzip
 101              $input->set('processingmethod', 'bz');
 102  
 103              if (!$input->open($archive))
 104              {
 105                  throw new \RuntimeException('Unable to read archive');
 106              }
 107  
 108              $output = Stream::getStream();
 109  
 110              if (!$output->open($destination, 'w'))
 111              {
 112                  $input->close();
 113  
 114                  throw new \RuntimeException('Unable to open file "' . $destination . '" for writing');
 115              }
 116  
 117              do
 118              {
 119                  $this->data = $input->read($input->get('chunksize', 8196));
 120  
 121                  if ($this->data)
 122                  {
 123                      if (!$output->write($this->data))
 124                      {
 125                          $input->close();
 126  
 127                          throw new \RuntimeException('Unable to write archive to file ' . $destination);
 128                      }
 129                  }
 130              }
 131              while ($this->data);
 132  
 133              $output->close();
 134              $input->close();
 135          }
 136  
 137          return true;
 138      }
 139  
 140      /**
 141       * Tests whether this adapter can unpack files on this computer.
 142       *
 143       * @return  boolean  True if supported
 144       *
 145       * @since   1.0
 146       */
 147  	public static function isSupported()
 148      {
 149          return \extension_loaded('bz2');
 150      }
 151  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer