Skip to content

Commit 48adb70

Browse files
committed
Improve error message on inconsistent ZIP files.
When using the graphical ZIP compress options on Mac OS or Ubuntu, the resulting ZIP archive will be inconsistent, tripping up `libzip`. With `ziptool` you could provoke the error to learn more about the inconsistency but that is unfortunately not available in PHP: ``` can't open zip archive 'a.zip': Zip archive inconsistent: entry 2: local and central headers do not match ``` `libzip` worked around this in nih-at/libzip@b3e3b19 which was released in https://github.com/nih-at/libzip/releases/tag/v1.10.1 but that version didn't make it into even unstable Debian/Ubuntu distributions yet.
1 parent 2b19b81 commit 48adb70

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

webapp/src/Service/DOMJudgeService.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -710,14 +710,18 @@ public function getCacheDir(): string
710710
public function openZipFile(string $filename): ZipArchive
711711
{
712712
$zip = new ZipArchive();
713-
$res = $zip->open($filename, ZIPARCHIVE::CHECKCONS);
714-
if ($res === ZIPARCHIVE::ER_NOZIP || $res === ZIPARCHIVE::ER_INCONS) {
715-
throw new BadRequestHttpException('No valid zip archive given');
713+
$res = $zip->open($filename, ZipArchive::CHECKCONS);
714+
if ($res === ZipArchive::ER_NOZIP) {
715+
throw new BadRequestHttpException('No valid ZIP archive given.');
716+
} elseif ($res === ZipArchive::ER_INCONS) {
717+
throw new BadRequestHttpException(
718+
'ZIP archive is inconsistent; this can happen when using built-in graphical ZIP tools on Mac OS or Ubuntu,'
719+
. ' use the command line zip tool instead, e.g.: zip -r ../problemarchive.zip *');
716720
} elseif ($res === ZIPARCHIVE::ER_MEMORY) {
717-
throw new ServiceUnavailableHttpException(null, 'Not enough memory to extract zip archive');
721+
throw new ServiceUnavailableHttpException(null, 'Not enough memory to extract ZIP archive.');
718722
} elseif ($res !== true) {
719723
throw new ServiceUnavailableHttpException(null,
720-
'Unknown error while extracting zip archive: ' . print_r($res, true));
724+
'Unknown error while extracting ZIP archive: ' . print_r($res, true));
721725
}
722726

723727
return $zip;

webapp/tests/Unit/Controller/API/SubmissionControllerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function provideAddInvalidData(): Generator
148148
['data' => 'aaa'],
149149
],
150150
],
151-
"No valid zip archive given"
151+
"No valid ZIP archive given."
152152
];
153153
yield [
154154
'demo',

0 commit comments

Comments
 (0)