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

https://bitbucket.org/hamidrezas/melobit · PHP · 245 lines · 126 code · 26 blank · 93 comment · 22 complexity · 83675805dcf86940bce7076b19f23929 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. * @version $Id: Tar.php 24594 2012-01-05 21:27:01Z matthew $
  20. */
  21. /**
  22. * @see Zend_Filter_Compress_CompressAbstract
  23. */
  24. require_once 'Zend/Filter/Compress/CompressAbstract.php';
  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 Zend_Filter_Compress_Tar extends Zend_Filter_Compress_CompressAbstract
  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. require_once 'Zend/Loader.php';
  58. try {
  59. Zend_Loader::loadClass('Archive_Tar');
  60. } catch (Zend_Exception $e) {
  61. require_once 'Zend/Filter/Exception.php';
  62. throw new Zend_Filter_Exception('This filter needs PEARs Archive_Tar', 0, $e);
  63. }
  64. }
  65. parent::__construct($options);
  66. }
  67. /**
  68. * Returns the set archive
  69. *
  70. * @return string
  71. */
  72. public function getArchive()
  73. {
  74. return $this->_options['archive'];
  75. }
  76. /**
  77. * Sets the archive to use for de-/compression
  78. *
  79. * @param string $archive Archive to use
  80. * @return Zend_Filter_Compress_Tar
  81. */
  82. public function setArchive($archive)
  83. {
  84. $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $archive);
  85. $this->_options['archive'] = (string) $archive;
  86. return $this;
  87. }
  88. /**
  89. * Returns the set targetpath
  90. *
  91. * @return string
  92. */
  93. public function getTarget()
  94. {
  95. return $this->_options['target'];
  96. }
  97. /**
  98. * Sets the targetpath to use
  99. *
  100. * @param string $target
  101. * @return Zend_Filter_Compress_Tar
  102. */
  103. public function setTarget($target)
  104. {
  105. if (!file_exists(dirname($target))) {
  106. require_once 'Zend/Filter/Exception.php';
  107. throw new Zend_Filter_Exception("The directory '$target' does not exist");
  108. }
  109. $target = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $target);
  110. $this->_options['target'] = (string) $target;
  111. return $this;
  112. }
  113. /**
  114. * Returns the set compression mode
  115. */
  116. public function getMode()
  117. {
  118. return $this->_options['mode'];
  119. }
  120. /**
  121. * Compression mode to use
  122. * Eighter Gz or Bz2
  123. *
  124. * @param string $mode
  125. */
  126. public function setMode($mode)
  127. {
  128. $mode = ucfirst(strtolower($mode));
  129. if (($mode != 'Bz2') && ($mode != 'Gz')) {
  130. require_once 'Zend/Filter/Exception.php';
  131. throw new Zend_Filter_Exception("The mode '$mode' is unknown");
  132. }
  133. if (($mode == 'Bz2') && (!extension_loaded('bz2'))) {
  134. require_once 'Zend/Filter/Exception.php';
  135. throw new Zend_Filter_Exception('This mode needs the bz2 extension');
  136. }
  137. if (($mode == 'Gz') && (!extension_loaded('zlib'))) {
  138. require_once 'Zend/Filter/Exception.php';
  139. throw new Zend_Filter_Exception('This mode needs the zlib extension');
  140. }
  141. }
  142. /**
  143. * Compresses the given content
  144. *
  145. * @param string $content
  146. * @return string
  147. */
  148. public function compress($content)
  149. {
  150. $archive = new Archive_Tar($this->getArchive(), $this->getMode());
  151. if (!file_exists($content)) {
  152. $file = $this->getTarget();
  153. if (is_dir($file)) {
  154. $file .= DIRECTORY_SEPARATOR . "tar.tmp";
  155. }
  156. $result = file_put_contents($file, $content);
  157. if ($result === false) {
  158. require_once 'Zend/Filter/Exception.php';
  159. throw new Zend_Filter_Exception('Error creating the temporary file');
  160. }
  161. $content = $file;
  162. }
  163. if (is_dir($content)) {
  164. // collect all file infos
  165. foreach (new RecursiveIteratorIterator(
  166. new RecursiveDirectoryIterator($content, RecursiveDirectoryIterator::KEY_AS_PATHNAME),
  167. RecursiveIteratorIterator::SELF_FIRST
  168. ) as $directory => $info
  169. ) {
  170. if ($info->isFile()) {
  171. $file[] = $directory;
  172. }
  173. }
  174. $content = $file;
  175. }
  176. $result = $archive->create($content);
  177. if ($result === false) {
  178. require_once 'Zend/Filter/Exception.php';
  179. throw new Zend_Filter_Exception('Error creating the Tar archive');
  180. }
  181. return $this->getArchive();
  182. }
  183. /**
  184. * Decompresses the given content
  185. *
  186. * @param string $content
  187. * @return boolean
  188. */
  189. public function decompress($content)
  190. {
  191. $archive = $this->getArchive();
  192. if (file_exists($content)) {
  193. $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, realpath($content));
  194. } elseif (empty($archive) || !file_exists($archive)) {
  195. require_once 'Zend/Filter/Exception.php';
  196. throw new Zend_Filter_Exception('Tar Archive not found');
  197. }
  198. $archive = new Archive_Tar($archive, $this->getMode());
  199. $target = $this->getTarget();
  200. if (!is_dir($target)) {
  201. $target = dirname($target);
  202. }
  203. $result = $archive->extract($target);
  204. if ($result === false) {
  205. require_once 'Zend/Filter/Exception.php';
  206. throw new Zend_Filter_Exception('Error while extracting the Tar archive');
  207. }
  208. return true;
  209. }
  210. /**
  211. * Returns the adapter name
  212. *
  213. * @return string
  214. */
  215. public function toString()
  216. {
  217. return 'Tar';
  218. }
  219. }