PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Filter/Compress/Rar.php

https://github.com/kervin/kyzstudio
PHP | 252 lines | 154 code | 13 blank | 85 comment | 5 complexity | e00605b4b10575734a75d06ea7ca26b8 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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Rar.php 22662 2010-07-24 17:37:36Z mabe $
  20. */
  21. /**
  22. * @see Zend_Filter_Compress_CompressAbstract
  23. */
  24. #require_once 'Zend/Filter/Compress/CompressAbstract.php';
  25. /**
  26. * Compression adapter for Rar
  27. *
  28. * @category Zend
  29. * @package Zend_Filter
  30. * @copyright Copyright (c) 2005-2010 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_Rar extends Zend_Filter_Compress_CompressAbstract
  34. {
  35. /**
  36. * Compression Options
  37. * array(
  38. * 'callback' => Callback for compression
  39. * 'archive' => Archive to use
  40. * 'password' => Password to use
  41. * 'target' => Target to write the files to
  42. * )
  43. *
  44. * @var array
  45. */
  46. protected $_options = array(
  47. 'callback' => null,
  48. 'archive' => null,
  49. 'password' => null,
  50. 'target' => '.',
  51. );
  52. /**
  53. * Class constructor
  54. *
  55. * @param array $options (Optional) Options to set
  56. */
  57. public function __construct($options = null)
  58. {
  59. if (!extension_loaded('rar')) {
  60. #require_once 'Zend/Filter/Exception.php';
  61. throw new Zend_Filter_Exception('This filter needs the rar extension');
  62. }
  63. parent::__construct($options);
  64. }
  65. /**
  66. * Returns the set callback for compression
  67. *
  68. * @return string
  69. */
  70. public function getCallback()
  71. {
  72. return $this->_options['callback'];
  73. }
  74. /**
  75. * Sets the callback to use
  76. *
  77. * @param string $callback
  78. * @return Zend_Filter_Compress_Rar
  79. */
  80. public function setCallback($callback)
  81. {
  82. if (!is_callable($callback)) {
  83. #require_once 'Zend/Filter/Exception.php';
  84. throw new Zend_Filter_Exception('Callback can not be accessed');
  85. }
  86. $this->_options['callback'] = $callback;
  87. return $this;
  88. }
  89. /**
  90. * Returns the set archive
  91. *
  92. * @return string
  93. */
  94. public function getArchive()
  95. {
  96. return $this->_options['archive'];
  97. }
  98. /**
  99. * Sets the archive to use for de-/compression
  100. *
  101. * @param string $archive Archive to use
  102. * @return Zend_Filter_Compress_Rar
  103. */
  104. public function setArchive($archive)
  105. {
  106. $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $archive);
  107. $this->_options['archive'] = (string) $archive;
  108. return $this;
  109. }
  110. /**
  111. * Returns the set password
  112. *
  113. * @return string
  114. */
  115. public function getPassword()
  116. {
  117. return $this->_options['password'];
  118. }
  119. /**
  120. * Sets the password to use
  121. *
  122. * @param string $password
  123. * @return Zend_Filter_Compress_Rar
  124. */
  125. public function setPassword($password)
  126. {
  127. $this->_options['password'] = (string) $password;
  128. return $this;
  129. }
  130. /**
  131. * Returns the set targetpath
  132. *
  133. * @return string
  134. */
  135. public function getTarget()
  136. {
  137. return $this->_options['target'];
  138. }
  139. /**
  140. * Sets the targetpath to use
  141. *
  142. * @param string $target
  143. * @return Zend_Filter_Compress_Rar
  144. */
  145. public function setTarget($target)
  146. {
  147. if (!file_exists(dirname($target))) {
  148. #require_once 'Zend/Filter/Exception.php';
  149. throw new Zend_Filter_Exception("The directory '$target' does not exist");
  150. }
  151. $target = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $target);
  152. $this->_options['target'] = (string) $target;
  153. return $this;
  154. }
  155. /**
  156. * Compresses the given content
  157. *
  158. * @param string|array $content
  159. * @return string
  160. */
  161. public function compress($content)
  162. {
  163. $callback = $this->getCallback();
  164. if ($callback === null) {
  165. #require_once 'Zend/Filter/Exception.php';
  166. throw new Zend_Filter_Exception('No compression callback available');
  167. }
  168. $options = $this->getOptions();
  169. unset($options['callback']);
  170. $result = call_user_func($callback, $options, $content);
  171. if ($result !== true) {
  172. #require_once 'Zend/Filter/Exception.php';
  173. throw new Zend_Filter_Exception('Error compressing the RAR Archive');
  174. }
  175. return $this->getArchive();
  176. }
  177. /**
  178. * Decompresses the given content
  179. *
  180. * @param string $content
  181. * @return boolean
  182. */
  183. public function decompress($content)
  184. {
  185. $archive = $this->getArchive();
  186. if (file_exists($content)) {
  187. $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, realpath($content));
  188. } elseif (empty($archive) || !file_exists($archive)) {
  189. #require_once 'Zend/Filter/Exception.php';
  190. throw new Zend_Filter_Exception('RAR Archive not found');
  191. }
  192. $password = $this->getPassword();
  193. if ($password !== null) {
  194. $archive = rar_open($archive, $password);
  195. } else {
  196. $archive = rar_open($archive);
  197. }
  198. if (!$archive) {
  199. #require_once 'Zend/Filter/Exception.php';
  200. throw new Zend_Filter_Exception("Error opening the RAR Archive");
  201. }
  202. $target = $this->getTarget();
  203. if (!is_dir($target)) {
  204. $target = dirname($target);
  205. }
  206. $filelist = rar_list($archive);
  207. if (!$filelist) {
  208. #require_once 'Zend/Filter/Exception.php';
  209. throw new Zend_Filter_Exception("Error reading the RAR Archive");
  210. }
  211. foreach($filelist as $file) {
  212. $file->extract($target);
  213. }
  214. rar_close($archive);
  215. return true;
  216. }
  217. /**
  218. * Returns the adapter name
  219. *
  220. * @return string
  221. */
  222. public function toString()
  223. {
  224. return 'Rar';
  225. }
  226. }