throw_if($relative==='',InvalidExtensionException::class,"Archive {$archive} contains a file without a name.");
File::ensureDirectoryExists(dirname($path));
$input=$zip->getStreamIndex($index);
$output=fopen($path,'wb');
throw_if($input===false||$output===false,InvalidExtensionException::class,"Unable to extract {$name} from {$archive}.");
try{
$crc=hash_init('crc32b');
while(($chunk=fread($input,65536))!==''){
throw_if($chunk===false,InvalidExtensionException::class,"Unable to extract {$name} from {$archive}.");
$written+=mb_strlen($chunk,'8bit');
throw_if($written>self::MAX_ARCHIVE_UNCOMPRESSED_BYTES,InvalidExtensionException::class,sprintf('Archive %s expands to more than the allowed %d bytes.',$archive,self::MAX_ARCHIVE_UNCOMPRESSED_BYTES));
throw_unless(fwrite($output,$chunk)===mb_strlen($chunk,'8bit'),InvalidExtensionException::class,"Unable to extract {$name} from {$archive}.");
hash_update($crc,$chunk);
}
throw_unless(hexdec(hash_final($crc))===$stat['crc'],InvalidExtensionException::class,"Entry {$name} of archive {$archive} is corrupt.");
}finally{
fclose($input);
fclose($output);
}
}
}
/**
* The entry's path relative to the extraction directory (empty for the directory itself).
* Names that would land outside it are refused rather than rewritten.
throw_if($zip->numFiles>self::MAX_ARCHIVE_ENTRIES,InvalidExtensionException::class,sprintf('Archive %s contains %d entries, more than the allowed %d.',$archive,$zip->numFiles,self::MAX_ARCHIVE_ENTRIES));
$total=0;
for($index=0;$index<$zip->numFiles;$index++){
$stat=$zip->statIndex($index);
throw_if($stat===false,InvalidExtensionException::class,"Unable to read entry {$index} of archive {$archive}.");
$total+=$stat['size'];
throw_if($total>self::MAX_ARCHIVE_UNCOMPRESSED_BYTES,InvalidExtensionException::class,sprintf('Archive %s expands to more than the allowed %d bytes.',$archive,self::MAX_ARCHIVE_UNCOMPRESSED_BYTES));