/libraries/joomla/archive/bzip2.php

https://bitbucket.org/pastor399/newcastleunifc · PHP · 176 lines · 117 code · 17 blank · 42 comment · 19 complexity · 7d3d517a1640fcd4ee12a505e556fb0e MD5 · raw file

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