Skip to content

Instantly share code, notes, and snippets.

@ssokolow
Last active March 12, 2023 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssokolow/234394255102aa3dfbe2b23017458dc1 to your computer and use it in GitHub Desktop.
Save ssokolow/234394255102aa3dfbe2b23017458dc1 to your computer and use it in GitHub Desktop.
PHP Code to check for Zip files which require ZIP64 support to unpack successfully
<?php
// NOTE: If your goal is to check for maximum compatibility
// rather than ZIP64 specifically, hedge against Zip
// implementations which use signed integer variables by
// changing 0xFFFF and 0xFFFFFFFF to 0x7FFF and 0x7FFFFFFF
// and then add a check for which compression algorithm
// each statIndex call says is in use.
function isZipSafe($path) {
// NOTE: Requires `php-zip` (Debian) or equivalent
$zipfile = new ZipArchive;
if ($zipfile->open($path, ZipArchive::RDONLY) === true) {
$count = $zipfile->count();
if ($count > 0xFFFF) {
print("ZIP64 based on number of entries\n");
$zipfile->close();
return false;
} elseif ($count == 0xFFFF) {
print("May be ZIP64 with a non-Zip64 libzip by number of entries\n");
$zipfile->close();
return false;
}
for ($i = 0; $i < $count; $i++) {
$size = $zipfile->statIndex($i)['size'];
$comp_size = $zipfile->statIndex($i)['comp_size'];
if ($size > 0xFFFFFFFF || $comp_size > 0xFFFFFFFF) {
print("ZIP64 based on file size\n");
$zipfile->close();
return false;
} elseif ($size == 0xFFFFFFFF || $comp_size == 0xFFFFFFFF) {
print("May be ZIP64 with a non-Zip64 libzip by file size\n");
$zipfile->close();
return false;
}
}
} else {
print("Failed to open " . $argv[1] . "\n");
return false;
}
return true;
}
isZipSafe($argv[1]);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment