Archive.php
5.34 KB
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
234
235
<?php
/**
* Part of the Joomla Framework Archive Package
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Archive;
use Joomla\Filesystem\File;
use Joomla\Filesystem\Folder;
/**
* An Archive handling class
*
* @since 1.0
*/
class Archive
{
/**
* The array of instantiated archive adapters.
*
* @var ExtractableInterface[]
* @since 1.0
*/
protected $adapters = array();
/**
* Holds the options array.
*
* @var array|\ArrayAccess
* @since 1.0
*/
public $options = array();
/**
* Create a new Archive object.
*
* @param array|\ArrayAccess $options An array of options
*
* @since 1.0
* @throws \InvalidArgumentException
*/
public function __construct($options = array())
{
if (!\is_array($options) && !($options instanceof \ArrayAccess))
{
throw new \InvalidArgumentException(
'The options param must be an array or implement the ArrayAccess interface.'
);
}
// Make sure we have a tmp directory.
isset($options['tmp_path']) || $options['tmp_path'] = realpath(sys_get_temp_dir());
$this->options = $options;
}
/**
* Extract an archive file to a directory.
*
* @param string $archivename The name of the archive file
* @param string $extractdir Directory to unpack into
*
* @return boolean True for success
*
* @since 1.0
* @throws \InvalidArgumentException
*/
public function extract($archivename, $extractdir)
{
$ext = pathinfo($archivename, PATHINFO_EXTENSION);
$path = pathinfo($archivename, PATHINFO_DIRNAME);
$filename = pathinfo($archivename, PATHINFO_FILENAME);
switch (strtolower($ext))
{
case 'zip':
$result = $this->getAdapter('zip')->extract($archivename, $extractdir);
break;
case 'tar':
$result = $this->getAdapter('tar')->extract($archivename, $extractdir);
break;
case 'tgz':
case 'gz':
case 'gzip':
// This may just be an individual file (e.g. sql script)
$tmpfname = $this->options['tmp_path'] . '/' . uniqid('gzip');
try
{
$this->getAdapter('gzip')->extract($archivename, $tmpfname);
}
catch (\RuntimeException $exception)
{
@unlink($tmpfname);
return false;
}
if ($ext === 'tgz' || stripos($filename, '.tar') !== false)
{
$result = $this->getAdapter('tar')->extract($tmpfname, $extractdir);
}
else
{
Folder::create($extractdir);
$result = File::copy($tmpfname, $extractdir . '/' . $filename, null, 0);
}
@unlink($tmpfname);
break;
case 'tbz2':
case 'bz2':
case 'bzip2':
// This may just be an individual file (e.g. sql script)
$tmpfname = $this->options['tmp_path'] . '/' . uniqid('bzip2');
try
{
$this->getAdapter('bzip2')->extract($archivename, $tmpfname);
}
catch (\RuntimeException $exception)
{
@unlink($tmpfname);
return false;
}
if ($ext === 'tbz2' || stripos($filename, '.tar') !== false)
{
$result = $this->getAdapter('tar')->extract($tmpfname, $extractdir);
}
else
{
Folder::create($extractdir);
$result = File::copy($tmpfname, $extractdir . '/' . $filename, null, 0);
}
@unlink($tmpfname);
break;
default:
throw new \InvalidArgumentException(sprintf('Unknown archive type: %s', $ext));
}
return $result;
}
/**
* Method to override the provided adapter with your own implementation.
*
* @param string $type Name of the adapter to set.
* @param string $class FQCN of your class which implements ExtractableInterface.
* @param boolean $override True to force override the adapter type.
*
* @return Archive This object for chaining.
*
* @since 1.0
* @throws \InvalidArgumentException
*/
public function setAdapter($type, $class, $override = true)
{
if ($override || !isset($this->adapters[$type]))
{
$error = !\is_object($class) && !class_exists($class)
? 'Archive adapter "%s" (class "%s") not found.'
: '';
$error = $error == '' && !($class instanceof ExtractableInterface)
? 'The provided adapter "%s" (class "%s") must implement Joomla\\Archive\\ExtractableInterface'
: $error;
$error = $error == '' && !$class::isSupported()
? 'Archive adapter "%s" (class "%s") not supported.'
: $error;
if ($error != '')
{
throw new \InvalidArgumentException(
sprintf($error, $type, $class)
);
}
$this->adapters[$type] = new $class($this->options);
}
return $this;
}
/**
* Get a file compression adapter.
*
* @param string $type The type of adapter (bzip2|gzip|tar|zip).
*
* @return ExtractableInterface Adapter for the requested type
*
* @since 1.0
* @throws \InvalidArgumentException
*/
public function getAdapter($type)
{
$type = strtolower($type);
if (!isset($this->adapters[$type]))
{
// Try to load the adapter object
/** @var ExtractableInterface $class */
$class = 'Joomla\\Archive\\' . ucfirst($type);
if (!class_exists($class) || !$class::isSupported())
{
throw new \InvalidArgumentException(
sprintf(
'Archive adapter "%s" (class "%s") not found or supported.',
$type,
$class
)
);
}
$this->adapters[$type] = new $class($this->options);
}
return $this->adapters[$type];
}
}