1
|
|
|
<?php |
2
|
|
|
namespace wapmorgan\UnifiedArchive\Formats; |
3
|
|
|
|
4
|
|
|
use Exception; |
5
|
|
|
use wapmorgan\UnifiedArchive\ArchiveEntry; |
6
|
|
|
use wapmorgan\UnifiedArchive\ArchiveInformation; |
7
|
|
|
|
8
|
|
|
class SevenZip extends BasicFormat |
9
|
|
|
{ |
10
|
|
|
/** @var Archive7z */ |
11
|
|
|
protected $sevenZip; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* BasicFormat constructor. |
15
|
|
|
* |
16
|
|
|
* @param string $archiveFileName |
17
|
|
|
* |
18
|
|
|
* @throws \Exception |
19
|
|
|
*/ |
20
|
3 |
|
public function __construct($archiveFileName) |
21
|
|
|
{ |
22
|
|
|
try { |
23
|
3 |
|
$this->sevenZip = new Archive7z($archiveFileName, null, null); |
24
|
|
|
} catch (\Archive7z\Exception $e) { |
|
|
|
|
25
|
|
|
throw new Exception('Could not open 7Zip archive: '.$e->getMessage(), $e->getCode(), $e); |
26
|
|
|
} |
27
|
3 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return ArchiveInformation |
31
|
|
|
*/ |
32
|
3 |
|
public function getArchiveInformation() |
33
|
|
|
{ |
34
|
3 |
|
$information = new ArchiveInformation(); |
35
|
3 |
|
foreach ($this->sevenZip->getEntries() as $entry) { |
36
|
3 |
|
$information->files[] = method_exists($entry, 'getUnixPath') |
37
|
3 |
|
? $entry->getUnixPath() |
38
|
|
|
: str_replace('\\', '/', $entry->getPath()); |
39
|
3 |
|
$information->compressedFilesSize += (int)$entry->getPackedSize(); |
40
|
3 |
|
$information->uncompressedFilesSize += (int)$entry->getSize(); |
41
|
|
|
} |
42
|
3 |
|
return $information; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
public function getFileNames() |
49
|
|
|
{ |
50
|
|
|
$files = []; |
51
|
|
|
foreach ($this->sevenZip->getEntries() as $entry) |
52
|
|
|
$files[] = $entry->getPath(); |
53
|
|
|
return $files; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $fileName |
58
|
|
|
* |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public function isFileExists($fileName) |
62
|
|
|
{ |
63
|
|
|
return $this->sevenZip->getEntry($fileName) !== null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $fileName |
68
|
|
|
* |
69
|
|
|
* @return ArchiveEntry|false |
70
|
|
|
*/ |
71
|
1 |
|
public function getFileData($fileName) |
72
|
|
|
{ |
73
|
1 |
|
$entry = $this->sevenZip->getEntry($fileName); |
74
|
1 |
|
return new ArchiveEntry($fileName, $entry->getPackedSize(), $entry->getSize(), |
75
|
1 |
|
strtotime($entry->getModified())); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $fileName |
80
|
|
|
* |
81
|
|
|
* @return string|false |
82
|
|
|
*/ |
83
|
1 |
|
public function getFileContent($fileName) |
84
|
|
|
{ |
85
|
1 |
|
$entry = $this->sevenZip->getEntry($fileName); |
86
|
1 |
|
return $entry->getContent(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $fileName |
91
|
|
|
* |
92
|
|
|
* @return bool|resource|string |
93
|
|
|
*/ |
94
|
1 |
|
public function getFileResource($fileName) |
95
|
|
|
{ |
96
|
1 |
|
$resource = fopen('php://temp', 'r+'); |
97
|
1 |
|
$entry = $this->sevenZip->getEntry($fileName); |
98
|
|
|
|
99
|
1 |
|
fwrite($resource, $entry->getContent()); |
|
|
|
|
100
|
1 |
|
rewind($resource); |
|
|
|
|
101
|
1 |
|
return $resource; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $outputFolder |
106
|
|
|
* @param array $files |
107
|
|
|
* |
108
|
|
|
* @return false|int |
109
|
|
|
* @throws \Archive7z\Exception |
110
|
|
|
* @throws \Exception |
111
|
|
|
*/ |
112
|
|
|
public function extractFiles($outputFolder, array $files) |
113
|
|
|
{ |
114
|
|
|
$this->sevenZip->setOutputDirectory($outputFolder); |
115
|
|
|
$count = 0; |
116
|
|
|
try { |
117
|
|
|
|
118
|
|
|
foreach ($files as $file) { |
119
|
|
|
$this->sevenZip->extractEntry($file); |
120
|
|
|
$count++; |
121
|
|
|
} |
122
|
|
|
return $count; |
123
|
|
|
} catch (Exception $e) { |
124
|
|
|
throw new Exception('Could not extract archive: '.$e->getMessage(), $e->getCode(), $e); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $outputFolder |
130
|
|
|
* |
131
|
|
|
* @return false|bool |
132
|
|
|
* @throws \Exception |
133
|
|
|
*/ |
134
|
|
|
public function extractArchive($outputFolder) |
135
|
|
|
{ |
136
|
|
|
$this->sevenZip->setOutputDirectory($outputFolder); |
137
|
|
|
try { |
138
|
|
|
$this->sevenZip->extract(); |
139
|
|
|
return true; |
|
|
|
|
140
|
|
|
} catch (Exception $e) { |
141
|
|
|
throw new Exception('Could not extract archive: '.$e->getMessage(), $e->getCode(), $e); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param array $files |
147
|
|
|
* |
148
|
|
|
* @return false|int |
149
|
|
|
* @throws \Exception |
150
|
|
|
*/ |
151
|
|
|
public function deleteFiles(array $files) |
152
|
|
|
{ |
153
|
|
|
$count = 0; |
154
|
|
|
try { |
155
|
|
|
foreach ($files as $file) { |
156
|
|
|
$this->sevenZip->delEntry($file); |
157
|
|
|
$count++; |
158
|
|
|
} |
159
|
|
|
return $count; |
160
|
|
|
} catch (Exception $e) { |
161
|
|
|
throw new Exception('Could not modify archive: '.$e->getMessage(), $e->getCode(), $e); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param array $files |
167
|
|
|
* |
168
|
|
|
* @return false|int |
169
|
|
|
* @throws \Exception |
170
|
|
|
*/ |
171
|
|
|
public function addFiles(array $files) |
172
|
|
|
{ |
173
|
|
|
$added_files = 0; |
174
|
|
|
try { |
175
|
|
|
foreach ($files as $localName => $filename) { |
176
|
|
|
if (!is_null($filename)) { |
177
|
|
|
$this->sevenZip->addEntry($filename); |
178
|
|
|
$this->sevenZip->renameEntry($filename, $localName); |
179
|
|
|
$added_files++; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
} catch (Exception $e) { |
183
|
|
|
throw new Exception('Could not modify archive: '.$e->getMessage(), $e->getCode(), $e); |
184
|
|
|
} |
185
|
|
|
return $added_files; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param array $files |
190
|
|
|
* @param string $archiveFileName |
191
|
|
|
* |
192
|
|
|
* @return false|int |
193
|
|
|
* @throws \Archive7z\Exception |
194
|
|
|
* @throws \Exception |
195
|
|
|
*/ |
196
|
|
|
public static function createArchive(array $files, $archiveFileName) { |
197
|
|
|
$seven_zip = new Archive7z($archiveFileName); |
198
|
|
|
try { |
199
|
|
|
foreach ($files as $localName => $filename) { |
200
|
|
|
if ($filename !== null) { |
201
|
|
|
$seven_zip->addEntry($filename, true); |
202
|
|
|
$seven_zip->renameEntry($filename, $localName); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
unset($seven_zip); |
206
|
|
|
} catch (Exception $e) { |
207
|
|
|
throw new Exception('Could not create archive: '.$e->getMessage(), $e->getCode(), $e); |
208
|
|
|
} |
209
|
|
|
return count($files); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @return bool |
214
|
|
|
* @throws \Archive7z\Exception |
215
|
|
|
*/ |
216
|
|
|
public static function canCreateArchive() |
217
|
|
|
{ |
218
|
|
|
return static::canAddFiles(); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @return bool |
223
|
|
|
* @throws \Archive7z\Exception |
224
|
|
|
*/ |
225
|
1 |
|
public static function canAddFiles() |
226
|
|
|
{ |
227
|
1 |
|
$version = Archive7z::getBinaryVersion(); |
228
|
1 |
|
return $version !== false && version_compare('9.30', $version, '<='); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @return bool |
233
|
|
|
*/ |
234
|
1 |
|
public static function canDeleteFiles() |
235
|
|
|
{ |
236
|
1 |
|
return true; |
237
|
|
|
} |
238
|
|
|
} |
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