Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 47 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
ZIP | |
0.00% |
0 / 47 |
|
0.00% |
0 / 4 |
72 | |
0.00% |
0 / 1 |
extract | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
readMeta | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
create | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
getErrorMessageText | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** @noinspection PhpUnhandledExceptionInspection */ |
3 | |
4 | class ZIP { |
5 | static function extract(string $zipPath, string $extractionPath): void { |
6 | $zip = new ZipArchive; |
7 | if ($errorCode = $zip->open($zipPath) !== true) { |
8 | throw new Exception('Could not extract archive:' . ZIP::getErrorMessageText($errorCode)); |
9 | } |
10 | $zip->extractTo($extractionPath . '/'); |
11 | $zip->close(); // TODO wrap inside finally block |
12 | } |
13 | |
14 | static function readMeta(string $zipPath): array { |
15 | $zip = new ZipArchive; |
16 | // ZIP can not be extracted in VFS-mode (api-tests), but with ZipArchive::CREATE an empty ZIP gets created instead |
17 | if ($errorCode = $zip->open($zipPath, ZipArchive::CREATE) !== true) { |
18 | throw new Exception('Could not read archive:' . ZIP::getErrorMessageText($errorCode)); |
19 | } |
20 | |
21 | try { |
22 | return [ |
23 | "comment" => $zip->getArchiveComment(ZipArchive::FL_UNCHANGED), |
24 | "count" => $zip->numFiles, |
25 | ]; |
26 | |
27 | } catch (Exception $exception) { |
28 | $zip->close(); |
29 | throw $exception; |
30 | } |
31 | } |
32 | |
33 | static function create(string $fileName): ZipArchive { |
34 | $zip = new ZipArchive; |
35 | $res = $zip->open($fileName, ZipArchive::CREATE); |
36 | if ($res === TRUE) { |
37 | return $zip; |
38 | } |
39 | $error = ZIP::getErrorMessageText($res); |
40 | throw new Exception("Could not create ZIP: `$error`"); |
41 | } |
42 | |
43 | static private function getErrorMessageText(int $errorCode): string { |
44 | return match ($errorCode) { |
45 | ZipArchive::ER_EXISTS => "File already exists", |
46 | ZipArchive::ER_INCONS => "Zip archive inconsistent.", |
47 | ZipArchive::ER_INVAL => "Invalid argument.", |
48 | ZipArchive::ER_MEMORY => "Malloc failure.", |
49 | ZipArchive::ER_NOENT => "No such file.", |
50 | ZipArchive::ER_NOZIP => "Not a zip archive.", |
51 | ZipArchive::ER_OPEN => "Can't open file.", |
52 | ZipArchive::ER_READ => "Read error.", |
53 | ZipArchive::ER_SEEK => "Seek error.", |
54 | ZipArchive::ER_MULTIDISK => "Multi-disk zip archives not supported.", |
55 | ZipArchive::ER_CLOSE => "Closing zip archive failed.", |
56 | ZipArchive::ER_RENAME => "Renaming temporary file failed.", |
57 | ZipArchive::ER_WRITE => "Write error.", |
58 | ZipArchive::ER_CRC => "CRC error.", |
59 | ZipArchive::ER_ZIPCLOSED => "Containing zip archive was closed.", |
60 | ZipArchive::ER_TMPOPEN => "Failure to create temporary file.", |
61 | ZipArchive::ER_ZLIB => "Zlib error.", |
62 | ZipArchive::ER_CHANGED => "Entry has been changed.", |
63 | ZipArchive::ER_COMPNOTSUPP => "Compression method not supported.", |
64 | ZipArchive::ER_EOF => "Premature EOF.", |
65 | ZipArchive::ER_INTERNAL => "Internal error.", |
66 | ZipArchive::ER_REMOVE => "Can't remove file.", |
67 | ZipArchive::ER_DELETED => "Entry has been deleted.", |
68 | default => "Unknown error: $errorCode", |
69 | }; |
70 | } |
71 | } |