note 119749 added to class.ziparchive
| From: | M.Wolf | Date: | Wed, 17 Aug 2016 05:39:17 +0000 |
| Subject: | note 119749 added to class.ziparchive | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-206307@lists.php.net to get a copy of this message | ||
How to detect corrupt files with CRC mismatch:
Creating a corrupt archive for testing is simple - zip some files and change a byte with a hex
editor in the resulting ZIP file. Now you can test the file with a ZIP application to learn which
file inside the archive is corrupt.
ZipArchive seems unable to detect broken files. ZipArchive::CHECKCONS doesn't help, only if
it's not a ZIP file at all. It happily decompressed corrupt files in my tests and the user
downloading the data is not informed.
You can simply verify on the server for smaller files:
<?php
$maxsize = 1024*1024;
$z = new ZipArchive;
$r = $z->open("foo.zip", ZipArchive::CHECKCONS);
if($r !== TRUE)
die('ZIP error when trying to open "foo.zip": '.$r);
$stat = $z->statName("mybrokenfile.txt");
if($stat['size'] > $maxsize)
die('File too large, decompression denied');
$s = $z->getStream($file);
$data = stream_get_contents($s, $maxsize);
fclose($s);
if($stat['crc'] != crc32($data))
die('File is corrupt!');
//echo 'File is valid';
//you may send the file to the client now if you didn't output anything before
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="mybrokenfile.txt"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . $stat['size']);
ob_clean();
echo $data;
$z->close();
?>
If the file shall not be fully decompressed on the server but decompressed while streaming to the
client due to it's size, the file transfer already startet and printing an error message later
doesn't work. Maybe the best way would be to interrupt the connection before closing the file
transfer. The client should be able to detect this as corrupt download.
On the server side a function is needed, that can calculate the CRC32 on streamed data stepwise.
----
Server IP: 72.52.91.14
Probable Submitter: 92.206.43.186
----
Manual Page -- http://php.net/manual/en/class.ziparchive.php
Edit -- https://master.php.net/note/edit/119749
Del: integrated -- https://master.php.net/note/delete/119749/integrated
Del: useless -- https://master.php.net/note/delete/119749/useless
Del: bad code -- https://master.php.net/note/delete/119749/bad+code
Del: spam -- https://master.php.net/note/delete/119749/spam
Del: non-english -- https://master.php.net/note/delete/119749/non-english
Del: in docs -- https://master.php.net/note/delete/119749/in+docs
Del: other reasons-- https://master.php.net/note/delete/119749
Reject -- https://master.php.net/note/reject/119749
Search -- https://master.php.net/manage/user-notes.php