This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Tar.php
233 lines (205 loc) · 6.34 KB
/
Tar.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Filter\Compress;
use Archive_Tar;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Zend\Filter\Exception;
/**
* Compression adapter for Tar
*/
class Tar extends AbstractCompressionAlgorithm
{
/**
* Compression Options
* array(
* 'archive' => Archive to use
* 'target' => Target to write the files to
* )
*
* @var array
*/
protected $options = [
'archive' => null,
'target' => '.',
'mode' => null,
];
/**
* Class constructor
*
* @param array $options (Optional) Options to set
* @throws Exception\ExtensionNotLoadedException if Archive_Tar component not available
*/
public function __construct($options = null)
{
if (! class_exists('Archive_Tar')) {
throw new Exception\ExtensionNotLoadedException(
'This filter needs PEAR\'s Archive_Tar component. '
. 'Ensure loading Archive_Tar (registering autoload or require_once)'
);
}
parent::__construct($options);
}
/**
* Returns the set archive
*
* @return string
*/
public function getArchive()
{
return $this->options['archive'];
}
/**
* Sets the archive to use for de-/compression
*
* @param string $archive Archive to use
* @return self
*/
public function setArchive($archive)
{
$archive = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, (string) $archive);
$this->options['archive'] = $archive;
return $this;
}
/**
* Returns the set target path
*
* @return string
*/
public function getTarget()
{
return $this->options['target'];
}
/**
* Sets the target path to use
*
* @param string $target
* @return self
* @throws Exception\InvalidArgumentException if target path does not exist
*/
public function setTarget($target)
{
if (! file_exists(dirname($target))) {
throw new Exception\InvalidArgumentException("The directory '$target' does not exist");
}
$target = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, (string) $target);
$this->options['target'] = $target;
return $this;
}
/**
* Returns the set compression mode
*
* @return string
*/
public function getMode()
{
return $this->options['mode'];
}
/**
* Compression mode to use
*
* Either Gz or Bz2.
*
* @param string $mode
* @return self
* @throws Exception\InvalidArgumentException for invalid $mode values
* @throws Exception\ExtensionNotLoadedException if bz2 mode selected but extension not loaded
* @throws Exception\ExtensionNotLoadedException if gz mode selected but extension not loaded
*/
public function setMode($mode)
{
$mode = strtolower($mode);
if ($mode !== 'bz2' && $mode !== 'gz') {
throw new Exception\InvalidArgumentException("The mode '$mode' is unknown");
}
if ($mode === 'bz2' && ! extension_loaded('bz2')) {
throw new Exception\ExtensionNotLoadedException('This mode needs the bz2 extension');
}
if ($mode === 'gz' && ! extension_loaded('zlib')) {
throw new Exception\ExtensionNotLoadedException('This mode needs the zlib extension');
}
$this->options['mode'] = $mode;
return $this;
}
/**
* Compresses the given content
*
* @param string $content
* @return string
* @throws Exception\RuntimeException if unable to create temporary file
* @throws Exception\RuntimeException if unable to create archive
*/
public function compress($content)
{
$archive = new Archive_Tar($this->getArchive(), $this->getMode());
if (! file_exists($content)) {
$file = $this->getTarget();
if (is_dir($file)) {
$file .= DIRECTORY_SEPARATOR . 'tar.tmp';
}
$result = file_put_contents($file, $content);
if ($result === false) {
throw new Exception\RuntimeException('Error creating the temporary file');
}
$content = $file;
}
if (is_dir($content)) {
// collect all file infos
foreach (new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($content, RecursiveDirectoryIterator::KEY_AS_PATHNAME),
RecursiveIteratorIterator::SELF_FIRST
) as $directory => $info) {
if ($info->isFile()) {
$file[] = $directory;
}
}
$content = $file;
}
$result = $archive->create($content);
if ($result === false) {
throw new Exception\RuntimeException('Error creating the Tar archive');
}
return $this->getArchive();
}
/**
* Decompresses the given content
*
* @param string $content
* @return string
* @throws Exception\RuntimeException if unable to find archive
* @throws Exception\RuntimeException if error occurs decompressing archive
*/
public function decompress($content)
{
$archive = $this->getArchive();
if (empty($archive) || ! file_exists($archive)) {
throw new Exception\RuntimeException('Tar Archive not found');
}
$archive = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, realpath($content));
$archive = new Archive_Tar($archive, $this->getMode());
$target = $this->getTarget();
if (! is_dir($target)) {
$target = dirname($target) . DIRECTORY_SEPARATOR;
}
$result = $archive->extract($target);
if ($result === false) {
throw new Exception\RuntimeException('Error while extracting the Tar archive');
}
return $target;
}
/**
* Returns the adapter name
*
* @return string
*/
public function toString()
{
return 'Tar';
}
}