PageRenderTime 21ms CodeModel.GetById 18ms app.highlight 2ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/archive/bzip2.php

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