note 106528 modified in function.ziparchive-statindex by danbrown

From: Date: Wed, 16 Nov 2011 16:39:35 +0000
Subject: note 106528 modified in function.ziparchive-statindex by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-184525@lists.php.net to get a copy of this message
The CRC returned from this method is a signed number and so can be negative. This isn't how most CRC's are represented and so can cause problems. Another thing to know is that the standerd CRC algorithm used for zip files is CRC32b. So to put this together lets say that you wanted to extract the first file from a zip and then compare the original CRC with the CRC for the extracted file this is what you would need to do. (proper error checking is omitted for simplicity). <?php $zip = new ZipArchive(); $zip->open("myZip.zip"); $stat = $zip->statIndex( 0 ); $name = $stat['name']; $oldCrc = $stat['crc']; $zip->extractTo("myPath", $name); $newCrc = hexdec(hash_file("crc32b", "myPath/" . $name)); // Have to test both cases as the unsigned CRC from within the zip might appear negative as a signed int. if($newCrc !== $oldCrc && ($oldCrc + 4294967296) !== $newCrc) { echo "The files don't match!"; } ?> --was-- The CRC returned from this method is a signed number and so can be negative. This isn't how most CRC's are represented and so can cause problems. Another thing to know is that the standerd CRC algorithm used for zip files is CRC32b. So to put this together lets say that you wanted to extract the first file from a zip and then compare the original CRC with the CRC for the extracted file this is what you would need to do. (proper error checking is omitted for simplicity). $zip = new ZipArchive(); $zip->open("myZip.zip"); $stat = $zip->statIndex( 0 ); $name = $stat['name']; $oldCrc = $stat['crc']; $zip->extractTo("myPath", $name); $newCrc = hexdec(hash_file("crc32b", "myPath/" . $name)); // Have to test both cases as the unsigned CRC from within the zip might appear negative as a signed int. if($newCrc !== $oldCrc && ($oldCrc + 4294967296) !== $newCrc) { echo "The files don't match!"; } http://php.net/manual/en/function.ziparchive-statindex.php

« previous php.notes (#184525) next »