A file archive, compressed with Zip.
Status of the Zip Archive
System status of the Zip Archive
Number of files in archive
File name in the file system
Comment for the archive
Be wary that there are several algorithms to generate a zip file. I found that Office OpenXML files created with ZipArchive are not recognized by Excel 2007, for example.
You have to use a different class to zip in this case, such as PclZip.
Read a file from an archive to a variable.
A warning is printed automatically in case of a CRC32 mismatch, which we capture, so we can print our own error message.
<?php
$zip = new ZipArchive();
if ($zip->open('archive.zip')) {
$fp = $zip->getStream('myfile.txt'); //file inside archive
if(!$fp)
die("Error: can't get stream to zipped file");
$stat = $zip->statName('myfile.txt');
$buf = ""; //file buffer
ob_start(); //to capture CRC error message
while (!feof($fp)) {
$buf .= fread($fp, 2048); //reading more than 2156 bytes seems to disable internal CRC32 verification (bug?)
}
$s = ob_get_contents();
ob_end_clean();
if(stripos($s, "CRC error") != FALSE){
echo 'CRC32 mismatch, current ';
printf("%08X", crc32($buf)); //current CRC
echo ', expected ';
printf("%08X", $stat['crc']); //expected CRC
}
fclose($fp);
$zip->close();
//Done, unpacked file is stored in $buf
}
?>
To create a corrupt file, change a byte in a zip file using a hex editor.