/library/Zend/Filter/Compress/Tar.php

https://github.com/jtai/zf2 · PHP · 248 lines · 119 code · 28 blank · 101 comment · 22 complexity · 2b75a6c89ac6b1f642583120282a5085 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Filter
  17. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. namespace Zend\Filter\Compress;
  21. use Archive_Tar,
  22. RecursiveDirectoryIterator,
  23. RecursiveIteratorIterator,
  24. Zend\Filter\Exception;
  25. /**
  26. * Compression adapter for Tar
  27. *
  28. * @category Zend
  29. * @package Zend_Filter
  30. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Tar extends AbstractCompressionAlgorithm
  34. {
  35. /**
  36. * Compression Options
  37. * array(
  38. * 'archive' => Archive to use
  39. * 'target' => Target to write the files to
  40. * )
  41. *
  42. * @var array
  43. */
  44. protected $options = array(
  45. 'archive' => null,
  46. 'target' => '.',
  47. 'mode' => null,
  48. );
  49. /**
  50. * Class constructor
  51. *
  52. * @param array $options (Optional) Options to set
  53. */
  54. public function __construct($options = null)
  55. {
  56. if (!class_exists('Archive_Tar')) {
  57. throw new Exception\ExtensionNotLoadedException(
  58. 'This filter needs PEAR\'s Archive_Tar component. '
  59. . 'Ensure loading Archive_Tar (registering autoload or require_once)');
  60. }
  61. parent::__construct($options);
  62. }
  63. /**
  64. * Returns the set archive
  65. *
  66. * @return string
  67. */
  68. public function getArchive()
  69. {
  70. return $this->options['archive'];
  71. }
  72. /**
  73. * Sets the archive to use for de-/compression
  74. *
  75. * @param string $archive Archive to use
  76. * @return Tar
  77. */
  78. public function setArchive($archive)
  79. {
  80. $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, (string) $archive);
  81. $this->options['archive'] = $archive;
  82. return $this;
  83. }
  84. /**
  85. * Returns the set target path
  86. *
  87. * @return string
  88. */
  89. public function getTarget()
  90. {
  91. return $this->options['target'];
  92. }
  93. /**
  94. * Sets the target path to use
  95. *
  96. * @param string $target
  97. * @return Tar
  98. * @throws Exception\InvalidArgumentException if target path does not exist
  99. */
  100. public function setTarget($target)
  101. {
  102. if (!file_exists(dirname($target))) {
  103. throw new Exception\InvalidArgumentException("The directory '$target' does not exist");
  104. }
  105. $target = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, (string) $target);
  106. $this->options['target'] = $target;
  107. return $this;
  108. }
  109. /**
  110. * Returns the set compression mode
  111. *
  112. * @return string
  113. */
  114. public function getMode()
  115. {
  116. return $this->options['mode'];
  117. }
  118. /**
  119. * Compression mode to use
  120. *
  121. * Either Gz or Bz2.
  122. *
  123. * @param string $mode
  124. * @return Tar
  125. * @throws Exception\InvalidArgumentException for invalid $mode values
  126. * @throws Exception\ExtensionNotLoadedException if bz2 mode selected but extension not loaded
  127. * @throws Exception\ExtensionNotLoadedException if gz mode selected but extension not loaded
  128. */
  129. public function setMode($mode)
  130. {
  131. $mode = ucfirst(strtolower($mode));
  132. if (($mode != 'Bz2') && ($mode != 'Gz')) {
  133. throw new Exception\InvalidArgumentException("The mode '$mode' is unknown");
  134. }
  135. if (($mode == 'Bz2') && (!extension_loaded('bz2'))) {
  136. throw new Exception\ExtensionNotLoadedException('This mode needs the bz2 extension');
  137. }
  138. if (($mode == 'Gz') && (!extension_loaded('zlib'))) {
  139. throw new Exception\ExtensionNotLoadedException('This mode needs the zlib extension');
  140. }
  141. $this->options['mode'] = $mode;
  142. return $this;
  143. }
  144. /**
  145. * Compresses the given content
  146. *
  147. * @param string $content
  148. * @return string
  149. * @throws Exception\RuntimeException if unable to create temporary file
  150. * @throws Exception\RuntimeException if unable to create archive
  151. */
  152. public function compress($content)
  153. {
  154. $archive = new Archive_Tar($this->getArchive(), $this->getMode());
  155. if (!file_exists($content)) {
  156. $file = $this->getTarget();
  157. if (is_dir($file)) {
  158. $file .= DIRECTORY_SEPARATOR . "tar.tmp";
  159. }
  160. $result = file_put_contents($file, $content);
  161. if ($result === false) {
  162. throw new Exception\RuntimeException('Error creating the temporary file');
  163. }
  164. $content = $file;
  165. }
  166. if (is_dir($content)) {
  167. // collect all file infos
  168. foreach (new RecursiveIteratorIterator(
  169. new RecursiveDirectoryIterator($content, RecursiveDirectoryIterator::KEY_AS_PATHNAME),
  170. RecursiveIteratorIterator::SELF_FIRST
  171. ) as $directory => $info
  172. ) {
  173. if ($info->isFile()) {
  174. $file[] = $directory;
  175. }
  176. }
  177. $content = $file;
  178. }
  179. $result = $archive->create($content);
  180. if ($result === false) {
  181. throw new Exception\RuntimeException('Error creating the Tar archive');
  182. }
  183. return $this->getArchive();
  184. }
  185. /**
  186. * Decompresses the given content
  187. *
  188. * @param string $content
  189. * @return boolean
  190. * @throws Exception\RuntimeException if unable to find archive
  191. * @throws Exception\RuntimeException if error occurs decompressing archive
  192. */
  193. public function decompress($content)
  194. {
  195. $archive = $this->getArchive();
  196. if (empty($archive) || !file_exists($archive)) {
  197. throw new Exception\RuntimeException('Tar Archive not found');
  198. }
  199. $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, realpath($content));
  200. $archive = new Archive_Tar($archive, $this->getMode());
  201. $target = $this->getTarget();
  202. if (!is_dir($target)) {
  203. $target = dirname($target);
  204. }
  205. $result = $archive->extract($target);
  206. if ($result === false) {
  207. throw new Exception\RuntimeException('Error while extracting the Tar archive');
  208. }
  209. return true;
  210. }
  211. /**
  212. * Returns the adapter name
  213. *
  214. * @return string
  215. */
  216. public function toString()
  217. {
  218. return 'Tar';
  219. }
  220. }