Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Filter / Compress / Tar.php
blob10a0cc15ff09ac782aaed586d42a661ba949f542
1 <?php
2 /**
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
8 */
10 namespace Zend\Filter\Compress;
12 use Archive_Tar;
13 use RecursiveDirectoryIterator;
14 use RecursiveIteratorIterator;
15 use Zend\Filter\Exception;
17 /**
18 * Compression adapter for Tar
20 class Tar extends AbstractCompressionAlgorithm
22 /**
23 * Compression Options
24 * array(
25 * 'archive' => Archive to use
26 * 'target' => Target to write the files to
27 * )
29 * @var array
31 protected $options = array(
32 'archive' => null,
33 'target' => '.',
34 'mode' => null,
37 /**
38 * Class constructor
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);
54 /**
55 * Returns the set archive
57 * @return string
59 public function getArchive()
61 return $this->options['archive'];
64 /**
65 * Sets the archive to use for de-/compression
67 * @param string $archive Archive to use
68 * @return self
70 public function setArchive($archive)
72 $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, (string) $archive);
73 $this->options['archive'] = $archive;
75 return $this;
78 /**
79 * Returns the set target path
81 * @return string
83 public function getTarget()
85 return $this->options['target'];
88 /**
89 * Sets the target path to use
91 * @param string $target
92 * @return self
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;
103 return $this;
107 * Returns the set compression mode
109 * @return string
111 public function getMode()
113 return $this->options['mode'];
117 * Compression mode to use
119 * Either Gz or Bz2.
121 * @param string $mode
122 * @return self
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;
143 return $this;
147 * Compresses the given content
149 * @param string $content
150 * @return string
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();
159 if (is_dir($file)) {
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');
168 $content = $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;
183 $content = $file;
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
198 * @return string
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');
221 return $target;
225 * Returns the adapter name
227 * @return string
229 public function toString()
231 return 'Tar';