3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Filter\Compress
;
13 use RecursiveDirectoryIterator
;
14 use RecursiveIteratorIterator
;
15 use Zend\Filter\Exception
;
18 * Compression adapter for Tar
20 class Tar
extends AbstractCompressionAlgorithm
25 * 'archive' => Archive to use
26 * 'target' => Target to write the files to
31 protected $options = array(
40 * @param array $options (Optional) Options to set
41 * @throws Exception\ExtensionNotLoadedException if Archive_Tar component not available
43 public function __construct($options = null)
45 if (!class_exists('Archive_Tar')) {
46 throw new Exception\
ExtensionNotLoadedException(
47 'This filter needs PEAR\'s Archive_Tar component. '
48 . 'Ensure loading Archive_Tar (registering autoload or require_once)');
51 parent
::__construct($options);
55 * Returns the set archive
59 public function getArchive()
61 return $this->options
['archive'];
65 * Sets the archive to use for de-/compression
67 * @param string $archive Archive to use
70 public function setArchive($archive)
72 $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR
, (string) $archive);
73 $this->options
['archive'] = $archive;
79 * Returns the set target path
83 public function getTarget()
85 return $this->options
['target'];
89 * Sets the target path to use
91 * @param string $target
93 * @throws Exception\InvalidArgumentException if target path does not exist
95 public function setTarget($target)
97 if (!file_exists(dirname($target))) {
98 throw new Exception\
InvalidArgumentException("The directory '$target' does not exist");
101 $target = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR
, (string) $target);
102 $this->options
['target'] = $target;
107 * Returns the set compression mode
111 public function getMode()
113 return $this->options
['mode'];
117 * Compression mode to use
121 * @param string $mode
123 * @throws Exception\InvalidArgumentException for invalid $mode values
124 * @throws Exception\ExtensionNotLoadedException if bz2 mode selected but extension not loaded
125 * @throws Exception\ExtensionNotLoadedException if gz mode selected but extension not loaded
127 public function setMode($mode)
129 $mode = ucfirst(strtolower($mode));
130 if (($mode != 'Bz2') && ($mode != 'Gz')) {
131 throw new Exception\
InvalidArgumentException("The mode '$mode' is unknown");
134 if (($mode == 'Bz2') && (!extension_loaded('bz2'))) {
135 throw new Exception\
ExtensionNotLoadedException('This mode needs the bz2 extension');
138 if (($mode == 'Gz') && (!extension_loaded('zlib'))) {
139 throw new Exception\
ExtensionNotLoadedException('This mode needs the zlib extension');
142 $this->options
['mode'] = $mode;
147 * Compresses the given content
149 * @param string $content
151 * @throws Exception\RuntimeException if unable to create temporary file
152 * @throws Exception\RuntimeException if unable to create archive
154 public function compress($content)
156 $archive = new Archive_Tar($this->getArchive(), $this->getMode());
157 if (!file_exists($content)) {
158 $file = $this->getTarget();
160 $file .= DIRECTORY_SEPARATOR
. "tar.tmp";
163 $result = file_put_contents($file, $content);
164 if ($result === false) {
165 throw new Exception\
RuntimeException('Error creating the temporary file');
171 if (is_dir($content)) {
172 // collect all file infos
173 foreach (new RecursiveIteratorIterator(
174 new RecursiveDirectoryIterator($content, RecursiveDirectoryIterator
::KEY_AS_PATHNAME
),
175 RecursiveIteratorIterator
::SELF_FIRST
176 ) as $directory => $info
178 if ($info->isFile()) {
179 $file[] = $directory;
186 $result = $archive->create($content);
187 if ($result === false) {
188 throw new Exception\
RuntimeException('Error creating the Tar archive');
191 return $this->getArchive();
195 * Decompresses the given content
197 * @param string $content
199 * @throws Exception\RuntimeException if unable to find archive
200 * @throws Exception\RuntimeException if error occurs decompressing archive
202 public function decompress($content)
204 $archive = $this->getArchive();
205 if (empty($archive) ||
!file_exists($archive)) {
206 throw new Exception\
RuntimeException('Tar Archive not found');
209 $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR
, realpath($content));
210 $archive = new Archive_Tar($archive, $this->getMode());
211 $target = $this->getTarget();
212 if (!is_dir($target)) {
213 $target = dirname($target) . DIRECTORY_SEPARATOR
;
216 $result = $archive->extract($target);
217 if ($result === false) {
218 throw new Exception\
RuntimeException('Error while extracting the Tar archive');
225 * Returns the adapter name
229 public function toString()