Last active
March 12, 2023 16:12
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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