in reply to File integrity checker

A corrupt file will throw an error and produce a non-zero return code on any attempt to decompress it, so you may as well just try to decompress the thing into a temporary location. If the return code is zero then the archive was intact and you can move the files to their permanent home. If not, discard the files that might have been extracted before the corruption was detected. There is no point in decompressing the file twice, which is effectively what would happen if you tried to test it first. It will be tested anyway.

Replies are listed 'Best First'.
Re^2: File integrity checker
by roperl (Beadle) on Aug 24, 2017 at 21:08 UTC
    Is there a method I can use to create a corrupted zip file, where just one member extractToFileNamed doesn't return AZ_OK?
      You can shoot out one of the files in a .zip with something like this:
      open my $ZIP, '+<', 'test.zip'; local $/ = undef; my $data = <$ZIP>; my @offset; while ($data =~ /PK\x03\x04/g) { # find all file headers push @offset, pos($data) - 4; } print "Found headers at: @offset\n"; substr($data, $offset[1]+14, 1) ^= "\x01"; # change the crc seek $ZIP, 0, 0; print $ZIP $data;
      (This isn't foolproof, but probably good enough.) The structure of a zip file is described here: Zip (file format)#File headers
        Ok so now I'm confused. I was able to corrupt the CRC on a zip file. The unzip utility run with the -t shows the corruption
        # unzip -t bad.zip Archive: bad.zip testing: testfile.txt OK testing: testfile2.txt bad CRC 32e1dbe6 (should be 32f +0dbe6) At least one error was detected in bad.zip.
        But when I run the ziptest.pl utility from the examples directory it shows the CRC as good
        # ./ziptest.pl bad.zip Length Size Last Modified CRC-32 Name -------- -------- ------------------------ -------- ---- 26 14 Mon Aug 28 18:07:58 2017 75b0ca95 testfile.txt 15 9 Mon Aug 28 18:08:10 2017 32e1dbe6 testfile2.txt All CRCs check OK
        So what exactly is $member->crc32 doing? Shouldn't it be checking the crc listing as expected in the zip file and not getting the CRC after the file is extracted?