1
|
|
|
<?php |
2
|
|
|
namespace wapmorgan\UnifiedArchive\Drivers; |
3
|
|
|
|
4
|
|
|
use CabArchive; |
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use wapmorgan\UnifiedArchive\ArchiveEntry; |
7
|
|
|
use wapmorgan\UnifiedArchive\ArchiveInformation; |
8
|
|
|
use wapmorgan\UnifiedArchive\Drivers\BasicDriver; |
9
|
|
|
use wapmorgan\UnifiedArchive\Exceptions\ArchiveExtractionException; |
10
|
|
|
use wapmorgan\UnifiedArchive\Exceptions\ArchiveModificationException; |
11
|
|
|
use wapmorgan\UnifiedArchive\Exceptions\UnsupportedOperationException; |
12
|
|
|
use wapmorgan\UnifiedArchive\Formats; |
13
|
|
|
|
14
|
|
|
class Cab extends BasicDriver |
15
|
|
|
{ |
16
|
|
|
const TYPE = self::TYPE_PURE_PHP; |
17
|
|
|
|
18
|
|
|
/** @var CabArchive */ |
19
|
|
|
protected $cab; |
20
|
|
|
|
21
|
|
|
/** |
22
|
1 |
|
* @inheritDoc |
23
|
|
|
*/ |
24
|
|
|
public static function getDescription() |
25
|
1 |
|
{ |
26
|
|
|
return 'php-library'; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public static function isInstalled() |
30
|
|
|
{ |
31
|
|
|
return class_exists('\CabArchive'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritDoc |
36
|
|
|
*/ |
37
|
|
|
public static function getInstallationInstruction() |
38
|
|
|
{ |
39
|
|
|
return 'install library [wapmorgan/cab-archive]: `composer require wapmorgan/cab-archive`'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public static function getSupportedFormats() |
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
|
|
Formats::CAB, |
49
|
|
|
]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param $format |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
public static function checkFormatSupport($format) |
57
|
|
|
{ |
58
|
|
|
if (!class_exists('\CabArchive')) { |
59
|
|
|
return []; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
switch ($format) { |
63
|
|
|
case Formats::CAB: |
64
|
|
|
$abilities = [ |
65
|
|
|
BasicDriver::OPEN, |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
$parts = explode('.', PHP_VERSION); |
69
|
|
|
// not supported on versions below 7.0.22, 7.1.8, 7.2.0 |
70
|
|
|
if ($parts[0] > 7 || $parts[1] >= 2 || (($parts[1] == 1 && $parts[2] >= 8) || ($parts[1] == 0 && $parts[2] >= 22))) { |
71
|
|
|
$abilities[] = BasicDriver::EXTRACT_CONTENT; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $abilities; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritDoc |
80
|
|
|
* @throws Exception |
81
|
|
|
*/ |
82
|
|
|
public function __construct($archiveFileName, $format, $password = null) |
83
|
|
|
{ |
84
|
|
|
parent::__construct($archiveFileName, $format); |
85
|
|
|
if ($password !== null) |
86
|
|
|
throw new UnsupportedOperationException('Cab archive does not support password!'); |
87
|
|
|
$this->open($archiveFileName); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Iso format destructor |
92
|
|
|
*/ |
93
|
|
|
public function __destruct() |
94
|
|
|
{ |
95
|
|
|
$this->cab = null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param $archiveFileName |
100
|
|
|
* @throws Exception |
101
|
|
|
*/ |
102
|
|
|
protected function open($archiveFileName) |
103
|
|
|
{ |
104
|
|
|
try { |
105
|
|
|
$this->cab = new CabArchive($archiveFileName); |
106
|
|
|
} catch (Exception $e) { |
107
|
|
|
throw new Exception('Could not open Cab archive: '.$e->getMessage(), $e->getCode(), $e); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return ArchiveInformation |
113
|
|
|
*/ |
114
|
|
|
public function getArchiveInformation() |
115
|
|
|
{ |
116
|
|
|
$information = new ArchiveInformation(); |
117
|
|
|
foreach ($this->cab->getFileNames() as $file) { |
118
|
|
|
$information->files[] = $file; |
119
|
|
|
$file_info = $this->cab->getFileData($file); |
120
|
|
|
$information->uncompressedFilesSize += $file_info->size; |
121
|
|
|
$information->compressedFilesSize += $file_info->packedSize; |
122
|
|
|
} |
123
|
|
|
return $information; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
public function getFileNames() |
130
|
|
|
{ |
131
|
|
|
return $this->cab->getFileNames(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $fileName |
136
|
|
|
* |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
|
|
public function isFileExists($fileName) |
140
|
|
|
{ |
141
|
|
|
return in_array($fileName, $this->cab->getFileNames(), true); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $fileName |
146
|
|
|
* |
147
|
|
|
* @return ArchiveEntry|false |
148
|
|
|
*/ |
149
|
|
|
public function getFileData($fileName) |
150
|
|
|
{ |
151
|
|
|
$data = $this->cab->getFileData($fileName); |
152
|
|
|
|
153
|
|
|
return new ArchiveEntry($fileName, $data->packedSize, $data->size, $data->unixtime, $data->is_compressed); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $fileName |
158
|
|
|
* |
159
|
|
|
* @return string|false |
160
|
|
|
* @throws Exception |
161
|
|
|
*/ |
162
|
|
|
public function getFileContent($fileName) |
163
|
|
|
{ |
164
|
|
|
return $this->cab->getFileContent($fileName); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param string $fileName |
169
|
|
|
* |
170
|
|
|
* @return bool|resource|string |
171
|
|
|
* @throws Exception |
172
|
|
|
*/ |
173
|
|
|
public function getFileStream($fileName) |
174
|
|
|
{ |
175
|
|
|
return self::wrapStringInStream($this->cab->getFileContent($fileName)); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param string $outputFolder |
180
|
|
|
* @param array $files |
181
|
|
|
* @return int Number of extracted files |
182
|
|
|
* @throws ArchiveExtractionException |
183
|
|
|
*/ |
184
|
|
|
public function extractFiles($outputFolder, array $files) |
185
|
|
|
{ |
186
|
|
|
try { |
187
|
|
|
return $this->cab->extract($outputFolder, $files); |
188
|
|
|
} catch (Exception $e) { |
189
|
|
|
throw new ArchiveExtractionException($e->getMessage(), |
190
|
|
|
$e->getCode(), |
191
|
|
|
$e->getPrevious() |
192
|
|
|
); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param string $outputFolder |
198
|
|
|
* @return int |
199
|
|
|
* @throws ArchiveExtractionException |
200
|
|
|
*/ |
201
|
|
|
public function extractArchive($outputFolder) |
202
|
|
|
{ |
203
|
|
|
try { |
204
|
|
|
return $this->cab->extract($outputFolder); |
205
|
|
|
} catch (Exception $e) { |
206
|
|
|
throw new ArchiveExtractionException($e->getMessage(), |
207
|
|
|
$e->getCode(), |
208
|
|
|
$e->getPrevious() |
209
|
|
|
); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
} |
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