1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace wapmorgan\UnifiedArchive\Drivers; |
4
|
|
|
|
5
|
|
|
use PhpZip\ZipFile; |
|
|
|
|
6
|
|
|
use wapmorgan\UnifiedArchive\ArchiveEntry; |
7
|
|
|
use wapmorgan\UnifiedArchive\ArchiveInformation; |
8
|
|
|
use wapmorgan\UnifiedArchive\Commands\BaseArchiveCommand; |
9
|
|
|
use wapmorgan\UnifiedArchive\Drivers\Basic\BasicDriver; |
10
|
|
|
use wapmorgan\UnifiedArchive\Drivers\Basic\BasicPureDriver; |
11
|
|
|
use wapmorgan\UnifiedArchive\Exceptions\ArchiveCreationException; |
12
|
|
|
use wapmorgan\UnifiedArchive\Exceptions\NonExistentArchiveFileException; |
13
|
|
|
use wapmorgan\UnifiedArchive\Exceptions\UnsupportedOperationException; |
14
|
|
|
use wapmorgan\UnifiedArchive\Formats; |
15
|
|
|
|
16
|
|
|
class NelexaZip extends BasicPureDriver |
17
|
|
|
{ |
18
|
|
|
const PACKAGE_NAME = 'nelexa/zip'; |
19
|
|
|
const MAIN_CLASS = '\\PhpZip\\ZipFile'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var ZipFile |
23
|
|
|
*/ |
24
|
|
|
protected $zip; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $files; |
30
|
|
|
|
31
|
|
|
public static function getDescription() |
32
|
|
|
{ |
33
|
|
|
return 'nelexa/zip driver'; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public static function getSupportedFormats() |
37
|
|
|
{ |
38
|
|
|
return [ |
39
|
|
|
Formats::ZIP, |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public static function checkFormatSupport($format) |
44
|
|
|
{ |
45
|
|
|
if (!static::isInstalled()) { |
46
|
|
|
return []; |
47
|
|
|
} |
48
|
|
|
return [ |
49
|
|
|
BasicDriver::OPEN, |
50
|
|
|
BasicDriver::OPEN_ENCRYPTED, |
51
|
|
|
BasicDriver::GET_COMMENT, |
52
|
|
|
BasicDriver::SET_COMMENT, |
53
|
|
|
BasicDriver::EXTRACT_CONTENT, |
54
|
|
|
BasicDriver::APPEND, |
55
|
|
|
BasicDriver::DELETE, |
56
|
|
|
BasicDriver::CREATE, |
57
|
|
|
BasicDriver::CREATE_ENCRYPTED, |
58
|
|
|
BasicDriver::CREATE_IN_STRING, |
59
|
|
|
]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param array $files |
64
|
|
|
* @param $archiveFileName |
65
|
|
|
* @param $archiveFormat |
66
|
|
|
* @param $compressionLevel |
67
|
|
|
* @param $password |
68
|
|
|
* @param $fileProgressCallable |
69
|
|
|
* @return int |
70
|
|
|
* @throws ArchiveCreationException |
71
|
|
|
* @throws UnsupportedOperationException |
72
|
|
|
*/ |
73
|
|
|
public static function createArchive( |
74
|
|
|
array $files, |
75
|
|
|
$archiveFileName, |
76
|
|
|
$archiveFormat, |
77
|
|
|
$compressionLevel = self::COMPRESSION_AVERAGE, |
78
|
|
|
$password = null, |
79
|
|
|
$fileProgressCallable = null) |
80
|
|
|
{ |
81
|
|
|
if ($fileProgressCallable !== null && !is_callable($fileProgressCallable)) { |
82
|
|
|
throw new ArchiveCreationException('File progress callable is not callable'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
try { |
86
|
|
|
$current_file = 0; |
87
|
|
|
$total_files = count($files); |
88
|
|
|
|
89
|
|
|
$zipFile = new \PhpZip\ZipFile(); |
90
|
|
|
foreach ($files as $archiveName => $localName) { |
91
|
|
|
$zipFile->addFile($archiveName, $archiveFormat); |
92
|
|
|
if ($fileProgressCallable !== null) { |
93
|
|
|
call_user_func_array($fileProgressCallable, [$current_file++, $total_files, $localName, $archiveName]); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
if ($password !== null) { |
97
|
|
|
$zipFile->setPassword($password); |
98
|
|
|
} |
99
|
|
|
$zipFile->saveAsFile($archiveFileName)->close(); |
100
|
|
|
} catch (\Exception $e) { |
101
|
|
|
throw new ArchiveCreationException('Could not create archive: '.$e->getMessage(), $e->getCode(), $e); |
102
|
|
|
} |
103
|
|
|
return count($files); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param array $files |
108
|
|
|
* @param string $archiveFormat |
109
|
|
|
* @param int $compressionLevel |
110
|
|
|
* @param string $password |
111
|
|
|
* @param callable|null $fileProgressCallable |
112
|
|
|
* @return string Content of archive |
113
|
|
|
* @throws ArchiveCreationException |
114
|
|
|
*/ |
115
|
|
|
public static function createArchiveInString( |
116
|
|
|
array $files, |
117
|
|
|
$archiveFormat, |
118
|
|
|
$compressionLevel = self::COMPRESSION_AVERAGE, |
119
|
|
|
$password = null, |
120
|
|
|
$fileProgressCallable = null |
121
|
|
|
) { |
122
|
|
|
try { |
123
|
|
|
$zipFile = new \PhpZip\ZipFile(); |
124
|
|
|
foreach ($files as $localName => $archiveName) { |
125
|
|
|
$zipFile->addFile($localName, $archiveFormat); |
126
|
|
|
} |
127
|
|
|
if ($password !== null) { |
128
|
|
|
$zipFile->setPassword($password); |
129
|
|
|
} |
130
|
|
|
return $zipFile->outputAsString(); |
131
|
|
|
} catch (\Exception $e) { |
132
|
|
|
throw new ArchiveCreationException('Could not create archive: '.$e->getMessage(), $e->getCode(), $e); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @inheritDoc |
138
|
|
|
* @throws \PhpZip\Exception\ZipException |
139
|
|
|
*/ |
140
|
|
|
public function __construct($archiveFileName, $format, $password = null) |
141
|
|
|
{ |
142
|
|
|
parent::__construct($archiveFileName, $format); |
143
|
|
|
$this->zip = new ZipFile(); |
144
|
|
|
$this->zip->openFile($archiveFileName); |
145
|
|
|
if ($password !== null) { |
146
|
|
|
$this->zip->setReadPassword($password); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @inheritDoc |
152
|
|
|
*/ |
153
|
|
|
public function getArchiveInformation() |
154
|
|
|
{ |
155
|
|
|
$this->files = []; |
156
|
|
|
$information = new ArchiveInformation(); |
157
|
|
|
|
158
|
|
|
$files = method_exists($this->zip, 'getAllInfo') |
159
|
|
|
? $this->zip->getAllInfo() |
160
|
|
|
: $this->zip->getEntries(); |
161
|
|
|
|
162
|
|
|
foreach ($files as $info) { |
163
|
|
|
if (method_exists($info, 'isFolder') ? $info->isFolder() : $info->isDirectory()) |
164
|
|
|
continue; |
165
|
|
|
|
166
|
|
|
$this->files[] = $information->files[] = str_replace('\\', '/', $info->getName()); |
167
|
|
|
$information->compressedFilesSize += $info->getCompressedSize(); |
168
|
|
|
$information->uncompressedFilesSize += method_exists($info, 'getSize') ? $info->getSize() : $info->getUncompressedSize(); |
169
|
|
|
} |
170
|
|
|
return $information; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @inheritDoc |
175
|
|
|
*/ |
176
|
|
|
public function getFileNames() |
177
|
|
|
{ |
178
|
|
|
return $this->files; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @inheritDoc |
183
|
|
|
*/ |
184
|
|
|
public function isFileExists($fileName) |
185
|
|
|
{ |
186
|
|
|
return $this->zip->hasEntry($fileName); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @inheritDoc |
191
|
|
|
*/ |
192
|
|
|
public function getFileData($fileName) |
193
|
|
|
{ |
194
|
|
|
$info = method_exists($this->zip, 'getEntryInfo') |
195
|
|
|
? $this->zip->getEntryInfo($fileName) |
196
|
|
|
: $this->zip->getEntry($fileName); |
197
|
|
|
|
198
|
|
|
return new ArchiveEntry( |
199
|
|
|
$fileName, |
200
|
|
|
$info->getCompressedSize(), |
201
|
|
|
method_exists($info, 'getSize') ? $info->getSize() : $info->getUncompressedSize(), |
202
|
|
|
$info->getMtime(), |
203
|
|
|
null, |
204
|
|
|
$info->getComment(), |
205
|
|
|
$info->getCrc() |
206
|
|
|
); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @inheritDoc |
211
|
|
|
*/ |
212
|
|
|
public function getFileContent($fileName) |
213
|
|
|
{ |
214
|
|
|
return $this->zip->getEntryContents($fileName); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @inheritDoc |
219
|
|
|
* @throws NonExistentArchiveFileException |
220
|
|
|
*/ |
221
|
|
|
public function getFileStream($fileName) |
222
|
|
|
{ |
223
|
|
|
return static::wrapStringInStream($this->getFileContent($fileName)); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @inheritDoc |
228
|
|
|
*/ |
229
|
|
|
public function extractFiles($outputFolder, array $files) |
230
|
|
|
{ |
231
|
|
|
$this->zip->extractTo($outputFolder, $files); |
232
|
|
|
return count($files); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @inheritDoc |
237
|
|
|
*/ |
238
|
|
|
public function extractArchive($outputFolder) |
239
|
|
|
{ |
240
|
|
|
$this->zip->extractTo($outputFolder); |
241
|
|
|
return count($this->files); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @inheritDoc |
246
|
|
|
* @throws \PhpZip\Exception\ZipException |
247
|
|
|
*/ |
248
|
|
|
public function addFileFromString($inArchiveName, $content) |
249
|
|
|
{ |
250
|
|
|
return $this->zip->addFromString($inArchiveName, $content); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
public function getComment() |
254
|
|
|
{ |
255
|
|
|
return $this->zip->getArchiveComment(); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function setComment($comment) |
259
|
|
|
{ |
260
|
|
|
return $this->zip->setArchiveComment($comment); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
public function deleteFiles(array $files) |
264
|
|
|
{ |
265
|
|
|
$deleted = 0; |
266
|
|
|
foreach ($files as $file) { |
267
|
|
|
$this->zip->deleteFromName($file); |
268
|
|
|
$deleted++; |
269
|
|
|
} |
270
|
|
|
return $deleted; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
public function addFiles(array $files) |
274
|
|
|
{ |
275
|
|
|
foreach ($files as $inArchiveName => $fsFileName) |
276
|
|
|
{ |
277
|
|
|
$this->zip->addFile($fsFileName, $inArchiveName); |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
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