|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace wapmorgan\UnifiedArchive\Drivers; |
|
4
|
|
|
|
|
5
|
|
|
use splitbrain\PHPArchive\Archive; |
|
|
|
|
|
|
6
|
|
|
use splitbrain\PHPArchive\ArchiveCorruptedException; |
|
|
|
|
|
|
7
|
|
|
use splitbrain\PHPArchive\ArchiveIllegalCompressionException; |
|
|
|
|
|
|
8
|
|
|
use splitbrain\PHPArchive\ArchiveIOException; |
|
|
|
|
|
|
9
|
|
|
use splitbrain\PHPArchive\FileInfo; |
|
|
|
|
|
|
10
|
|
|
use splitbrain\PHPArchive\FileInfoException; |
|
|
|
|
|
|
11
|
|
|
use splitbrain\PHPArchive\Tar; |
|
|
|
|
|
|
12
|
|
|
use splitbrain\PHPArchive\Zip; |
|
|
|
|
|
|
13
|
|
|
use wapmorgan\UnifiedArchive\Abilities; |
|
14
|
|
|
use wapmorgan\UnifiedArchive\ArchiveEntry; |
|
15
|
|
|
use wapmorgan\UnifiedArchive\ArchiveInformation; |
|
16
|
|
|
use wapmorgan\UnifiedArchive\Drivers\Basic\BasicPureDriver; |
|
17
|
|
|
use wapmorgan\UnifiedArchive\Exceptions\UnsupportedOperationException; |
|
18
|
|
|
use wapmorgan\UnifiedArchive\Formats; |
|
19
|
|
|
|
|
20
|
|
|
class SplitbrainPhpArchive extends BasicPureDriver |
|
21
|
|
|
{ |
|
22
|
|
|
const PACKAGE_NAME = 'splitbrain/php-archive'; |
|
23
|
|
|
const MAIN_CLASS = '\\splitbrain\\PHPArchive\\Archive'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var \splitbrain\PHPArchive\Zip|Tar |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $archive; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $files; |
|
33
|
|
|
|
|
34
|
|
|
/** @var FileInfo[] */ |
|
35
|
|
|
protected $members; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @inheritDoc |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function getDescription() |
|
41
|
|
|
{ |
|
42
|
|
|
return 'php-library for zip/tar (with gzip/bzip-compression)'; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @inheritDoc |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function getFormats() |
|
49
|
|
|
{ |
|
50
|
|
|
return [ |
|
51
|
|
|
Formats::ZIP, |
|
52
|
|
|
Formats::TAR, |
|
53
|
|
|
Formats::TAR_GZIP, |
|
54
|
|
|
Formats::TAR_BZIP, |
|
55
|
|
|
]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @inheritDoc |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function getFormatAbilities($format) |
|
62
|
|
|
{ |
|
63
|
|
|
if (!static::isInstalled()) { |
|
64
|
|
|
return []; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ( |
|
68
|
|
|
($format === Formats::TAR_BZIP && !extension_loaded('bz2')) |
|
69
|
|
|
|| ($format === Formats::TAR_GZIP && !extension_loaded('zlib')) |
|
70
|
|
|
) { |
|
71
|
|
|
return []; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
switch ($format) { |
|
75
|
|
|
case Formats::ZIP: |
|
76
|
|
|
case Formats::TAR: |
|
77
|
|
|
case Formats::TAR_GZIP: |
|
78
|
|
|
case Formats::TAR_BZIP: |
|
79
|
|
|
return [ |
|
80
|
|
|
Abilities::OPEN, |
|
81
|
|
|
// BasicDriver::EXTRACT_CONTENT, |
|
82
|
|
|
Abilities::APPEND, |
|
83
|
|
|
Abilities::CREATE, |
|
84
|
|
|
// BasicDriver::CREATE_ENCRYPTED, |
|
85
|
|
|
Abilities::CREATE_IN_STRING, |
|
86
|
|
|
]; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @throws ArchiveIllegalCompressionException |
|
92
|
|
|
* @throws ArchiveIOException |
|
93
|
|
|
*/ |
|
94
|
|
|
public function __construct($archiveFileName, $format, $password = null) |
|
95
|
|
|
{ |
|
96
|
|
|
parent::__construct($archiveFileName, $format, $password); |
|
97
|
|
|
if ($format === Formats::ZIP) { |
|
98
|
|
|
$this->archive = new \splitbrain\PHPArchive\Zip(); |
|
99
|
|
|
} else { |
|
100
|
|
|
$this->archive = new Tar(); |
|
101
|
|
|
} |
|
102
|
|
|
$this->archive->open($archiveFileName); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @inheritDoc |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getArchiveInformation() |
|
109
|
|
|
{ |
|
110
|
|
|
$this->files = []; |
|
111
|
|
|
$information = new ArchiveInformation(); |
|
112
|
|
|
|
|
113
|
|
|
foreach ($this->archive->contents() as $member) { |
|
114
|
|
|
if ($member->getIsdir()) { |
|
115
|
|
|
continue; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$this->files[] = $information->files[] = str_replace('\\', '/', $member->getPath()); |
|
119
|
|
|
$this->members[str_replace('\\', '/', $member->getPath())] = $member; |
|
120
|
|
|
$information->compressedFilesSize += (int)$member->getCompressedSize(); |
|
121
|
|
|
$information->uncompressedFilesSize += (int)$member->getSize(); |
|
122
|
|
|
} |
|
123
|
|
|
return $information; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @inheritDoc |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getFileNames() |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->files; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @inheritDoc |
|
136
|
|
|
*/ |
|
137
|
|
|
public function isFileExists($fileName) |
|
138
|
|
|
{ |
|
139
|
|
|
return array_key_exists($fileName, $this->members); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @inheritDoc |
|
144
|
|
|
*/ |
|
145
|
|
|
public function getFileData($fileName) |
|
146
|
|
|
{ |
|
147
|
|
|
$entry = $this->members[$fileName]; |
|
148
|
|
|
return new ArchiveEntry( |
|
149
|
|
|
$fileName, |
|
150
|
|
|
$entry->getCompressedSize(), |
|
151
|
|
|
$entry->getSize(), |
|
152
|
|
|
strtotime($entry->getMtime()), |
|
153
|
|
|
$entry->getSize() !== $entry->getCompressedSize(), |
|
154
|
|
|
$entry->getComment(), |
|
155
|
|
|
null |
|
156
|
|
|
); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @inheritDoc |
|
161
|
|
|
* @throws UnsupportedOperationException |
|
162
|
|
|
*/ |
|
163
|
|
|
public function getFileContent($fileName) |
|
164
|
|
|
{ |
|
165
|
|
|
throw new UnsupportedOperationException('Getting file content is not supported by ' . __CLASS__); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @inheritDoc |
|
170
|
|
|
* @throws UnsupportedOperationException |
|
171
|
|
|
*/ |
|
172
|
|
|
public function getFileStream($fileName) |
|
173
|
|
|
{ |
|
174
|
|
|
throw new UnsupportedOperationException('Getting file stream is not supported by ' . __CLASS__); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @inheritDoc |
|
179
|
|
|
* @throws UnsupportedOperationException |
|
180
|
|
|
*/ |
|
181
|
|
|
public function extractFiles($outputFolder, array $files) |
|
182
|
|
|
{ |
|
183
|
|
|
throw new UnsupportedOperationException('Extract specific files is not supported by ' . __CLASS__); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @inheritDoc |
|
188
|
|
|
*/ |
|
189
|
|
|
public function extractArchive($outputFolder) |
|
190
|
|
|
{ |
|
191
|
|
|
$this->archive->extract($outputFolder); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @param array $files |
|
196
|
|
|
* @param string $archiveFileName |
|
197
|
|
|
* @param int $archiveFormat |
|
198
|
|
|
* @param int $compressionLevel |
|
199
|
|
|
* @param string|null $password |
|
200
|
|
|
* @param callable|null $fileProgressCallable |
|
201
|
|
|
* @return int Number of archived files |
|
202
|
|
|
* @throws ArchiveCorruptedException |
|
203
|
|
|
* @throws ArchiveIOException |
|
204
|
|
|
* @throws ArchiveIllegalCompressionException |
|
205
|
|
|
* @throws FileInfoException |
|
206
|
|
|
* @throws UnsupportedOperationException |
|
207
|
|
|
*/ |
|
208
|
|
|
public static function createArchive( |
|
209
|
|
|
array $files, |
|
210
|
|
|
$archiveFileName, |
|
211
|
|
|
$archiveFormat, |
|
212
|
|
|
$compressionLevel = self::COMPRESSION_AVERAGE, |
|
213
|
|
|
$password = null, |
|
214
|
|
|
$fileProgressCallable = null |
|
215
|
|
|
) { |
|
216
|
|
|
if ($password !== null) { |
|
217
|
|
|
throw new UnsupportedOperationException(__CLASS__ . ' could not encrypt an archive'); |
|
218
|
|
|
} |
|
219
|
|
|
$archive = static::createArchiveInternal($files, $archiveFileName, $archiveFormat, $compressionLevel, $fileProgressCallable); |
|
|
|
|
|
|
220
|
|
|
$archive->save($archiveFileName); |
|
221
|
|
|
return count($files); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @param array $files |
|
226
|
|
|
* @param string $archiveFormat |
|
227
|
|
|
* @param int $compressionLevel |
|
228
|
|
|
* @param string $password |
|
229
|
|
|
* @param callable|null $fileProgressCallable |
|
230
|
|
|
* @return string Content of archive |
|
231
|
|
|
* @throws ArchiveCorruptedException |
|
232
|
|
|
* @throws ArchiveIOException |
|
233
|
|
|
* @throws ArchiveIllegalCompressionException |
|
234
|
|
|
* @throws FileInfoException |
|
235
|
|
|
* @throws UnsupportedOperationException |
|
236
|
|
|
*/ |
|
237
|
|
|
public static function createArchiveInString( |
|
238
|
|
|
array $files, |
|
239
|
|
|
$archiveFormat, |
|
240
|
|
|
$compressionLevel = self::COMPRESSION_AVERAGE, |
|
241
|
|
|
$password = null, |
|
242
|
|
|
$fileProgressCallable = null |
|
243
|
|
|
) { |
|
244
|
|
|
if ($password !== null) { |
|
245
|
|
|
throw new UnsupportedOperationException(__CLASS__ . ' could not encrypt an archive'); |
|
246
|
|
|
} |
|
247
|
|
|
$archive = static::createArchiveInternal($files, null, $archiveFormat, $compressionLevel, $fileProgressCallable); |
|
|
|
|
|
|
248
|
|
|
return $archive->getArchive(); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* @param array $files |
|
253
|
|
|
* @param $archiveFileName |
|
254
|
|
|
* @param $archiveFormat |
|
255
|
|
|
* @param int $compressionLevel |
|
256
|
|
|
* @param null $fileProgressCallable |
|
|
|
|
|
|
257
|
|
|
* @return Tar|Zip |
|
258
|
|
|
* @throws ArchiveCorruptedException |
|
259
|
|
|
* @throws ArchiveIOException |
|
260
|
|
|
* @throws ArchiveIllegalCompressionException |
|
261
|
|
|
* @throws FileInfoException |
|
262
|
|
|
*/ |
|
263
|
|
|
public static function createArchiveInternal( |
|
264
|
|
|
array $files, |
|
265
|
|
|
$archiveFileName, |
|
266
|
|
|
$archiveFormat, |
|
267
|
|
|
$compressionLevel = self::COMPRESSION_AVERAGE, |
|
268
|
|
|
$fileProgressCallable = null |
|
269
|
|
|
) { |
|
270
|
|
|
static $compressionLevelMap = [ |
|
271
|
|
|
self::COMPRESSION_NONE => 0, |
|
272
|
|
|
self::COMPRESSION_WEAK => 2, |
|
273
|
|
|
self::COMPRESSION_AVERAGE => 4, |
|
274
|
|
|
self::COMPRESSION_STRONG => 7, |
|
275
|
|
|
self::COMPRESSION_MAXIMUM => 9, |
|
276
|
|
|
]; |
|
277
|
|
|
|
|
278
|
|
|
if ($archiveFormat === Formats::ZIP) { |
|
279
|
|
|
$archive = new \splitbrain\PHPArchive\Zip(); |
|
280
|
|
|
$archive->setCompression($compressionLevelMap[$compressionLevel], Archive::COMPRESS_AUTO); |
|
281
|
|
|
} else { |
|
282
|
|
|
$archive = new Tar(); |
|
283
|
|
|
$archive->setCompression($compressionLevelMap[$compressionLevel], ($archiveFormat === Formats::TAR_BZIP |
|
284
|
|
|
? Archive::COMPRESS_BZIP |
|
285
|
|
|
: ($archiveFormat === Formats::TAR_GZIP ? Archive::COMPRESS_GZIP : Archive::COMPRESS_NONE))); |
|
286
|
|
|
} |
|
287
|
|
|
$archive->create($archiveFileName); |
|
288
|
|
|
|
|
289
|
|
|
$current_file = 0; |
|
290
|
|
|
$total_files = count($files); |
|
291
|
|
|
foreach ($files as $inArchiveName => $localName) { |
|
292
|
|
|
$archive->addFile($localName, $inArchiveName); |
|
293
|
|
|
if ($fileProgressCallable !== null) { |
|
294
|
|
|
call_user_func_array($fileProgressCallable, [$current_file++, $total_files, $localName, $inArchiveName]); |
|
|
|
|
|
|
295
|
|
|
} |
|
296
|
|
|
} |
|
297
|
|
|
return $archive; |
|
298
|
|
|
} |
|
299
|
|
|
} |
|
300
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths