/library/Zend/Filter/Compress/Tar.php
PHP | 245 lines | 150 code | 18 blank | 77 comment | 19 complexity | 83675805dcf86940bce7076b19f23929 MD5 | raw file
Possible License(s): AGPL-1.0
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/**
23 * @see Zend_Filter_Compress_CompressAbstract
24 */
25require_once 'Zend/Filter/Compress/CompressAbstract.php';
26
27/**
28 * Compression adapter for Tar
29 *
30 * @category Zend
31 * @package Zend_Filter
32 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
33 * @license http://framework.zend.com/license/new-bsd New BSD License
34 */
35class Zend_Filter_Compress_Tar extends Zend_Filter_Compress_CompressAbstract
36{
37 /**
38 * Compression Options
39 * array(
40 * 'archive' => Archive to use
41 * 'target' => Target to write the files to
42 * )
43 *
44 * @var array
45 */
46 protected $_options = array(
47 'archive' => null,
48 'target' => '.',
49 'mode' => null,
50 );
51
52 /**
53 * Class constructor
54 *
55 * @param array $options (Optional) Options to set
56 */
57 public function __construct($options = null)
58 {
59 if (!class_exists('Archive_Tar')) {
60 require_once 'Zend/Loader.php';
61 try {
62 Zend_Loader::loadClass('Archive_Tar');
63 } catch (Zend_Exception $e) {
64 require_once 'Zend/Filter/Exception.php';
65 throw new Zend_Filter_Exception('This filter needs PEARs Archive_Tar', 0, $e);
66 }
67 }
68
69 parent::__construct($options);
70 }
71
72 /**
73 * Returns the set archive
74 *
75 * @return string
76 */
77 public function getArchive()
78 {
79 return $this->_options['archive'];
80 }
81
82 /**
83 * Sets the archive to use for de-/compression
84 *
85 * @param string $archive Archive to use
86 * @return Zend_Filter_Compress_Tar
87 */
88 public function setArchive($archive)
89 {
90 $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $archive);
91 $this->_options['archive'] = (string) $archive;
92
93 return $this;
94 }
95
96 /**
97 * Returns the set targetpath
98 *
99 * @return string
100 */
101 public function getTarget()
102 {
103 return $this->_options['target'];
104 }
105
106 /**
107 * Sets the targetpath to use
108 *
109 * @param string $target
110 * @return Zend_Filter_Compress_Tar
111 */
112 public function setTarget($target)
113 {
114 if (!file_exists(dirname($target))) {
115 require_once 'Zend/Filter/Exception.php';
116 throw new Zend_Filter_Exception("The directory '$target' does not exist");
117 }
118
119 $target = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $target);
120 $this->_options['target'] = (string) $target;
121 return $this;
122 }
123
124 /**
125 * Returns the set compression mode
126 */
127 public function getMode()
128 {
129 return $this->_options['mode'];
130 }
131
132 /**
133 * Compression mode to use
134 * Eighter Gz or Bz2
135 *
136 * @param string $mode
137 */
138 public function setMode($mode)
139 {
140 $mode = ucfirst(strtolower($mode));
141 if (($mode != 'Bz2') && ($mode != 'Gz')) {
142 require_once 'Zend/Filter/Exception.php';
143 throw new Zend_Filter_Exception("The mode '$mode' is unknown");
144 }
145
146 if (($mode == 'Bz2') && (!extension_loaded('bz2'))) {
147 require_once 'Zend/Filter/Exception.php';
148 throw new Zend_Filter_Exception('This mode needs the bz2 extension');
149 }
150
151 if (($mode == 'Gz') && (!extension_loaded('zlib'))) {
152 require_once 'Zend/Filter/Exception.php';
153 throw new Zend_Filter_Exception('This mode needs the zlib extension');
154 }
155 }
156
157 /**
158 * Compresses the given content
159 *
160 * @param string $content
161 * @return string
162 */
163 public function compress($content)
164 {
165 $archive = new Archive_Tar($this->getArchive(), $this->getMode());
166 if (!file_exists($content)) {
167 $file = $this->getTarget();
168 if (is_dir($file)) {
169 $file .= DIRECTORY_SEPARATOR . "tar.tmp";
170 }
171
172 $result = file_put_contents($file, $content);
173 if ($result === false) {
174 require_once 'Zend/Filter/Exception.php';
175 throw new Zend_Filter_Exception('Error creating the temporary file');
176 }
177
178 $content = $file;
179 }
180
181 if (is_dir($content)) {
182 // collect all file infos
183 foreach (new RecursiveIteratorIterator(
184 new RecursiveDirectoryIterator($content, RecursiveDirectoryIterator::KEY_AS_PATHNAME),
185 RecursiveIteratorIterator::SELF_FIRST
186 ) as $directory => $info
187 ) {
188 if ($info->isFile()) {
189 $file[] = $directory;
190 }
191 }
192
193 $content = $file;
194 }
195
196 $result = $archive->create($content);
197 if ($result === false) {
198 require_once 'Zend/Filter/Exception.php';
199 throw new Zend_Filter_Exception('Error creating the Tar archive');
200 }
201
202 return $this->getArchive();
203 }
204
205 /**
206 * Decompresses the given content
207 *
208 * @param string $content
209 * @return boolean
210 */
211 public function decompress($content)
212 {
213 $archive = $this->getArchive();
214 if (file_exists($content)) {
215 $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, realpath($content));
216 } elseif (empty($archive) || !file_exists($archive)) {
217 require_once 'Zend/Filter/Exception.php';
218 throw new Zend_Filter_Exception('Tar Archive not found');
219 }
220
221 $archive = new Archive_Tar($archive, $this->getMode());
222 $target = $this->getTarget();
223 if (!is_dir($target)) {
224 $target = dirname($target);
225 }
226
227 $result = $archive->extract($target);
228 if ($result === false) {
229 require_once 'Zend/Filter/Exception.php';
230 throw new Zend_Filter_Exception('Error while extracting the Tar archive');
231 }
232
233 return true;
234 }
235
236 /**
237 * Returns the adapter name
238 *
239 * @return string
240 */
241 public function toString()
242 {
243 return 'Tar';
244 }
245}