http://qs1969.pair.com?node_id=1197942


in reply to File integrity checker

Would like to check the integrity of the file before attempting uncompression or extraction.
There's not really any way to do this. Zip and gzip store checksums of the uncompressed contents; the -t options actually uncompress the file, calculate the checksum, and throw away the contents. You could do the same thing in your program, or you could uncompress to a tempfile and delete it if the checksum comes out bad.

Edit: There's a section about integrity checking in the Archive::Zip::FAQ.

Replies are listed 'Best First'.
Re^2: File integrity checker
by roperl (Beadle) on Aug 24, 2017 at 16:49 UTC
    Thanks, yes the integrity check section in Archive::Zip::FAQ seems to be exactly what I need for zip files
    I'm already gunzipping the gz files to a temp file name. So how do would I get the the checksum of the uncompressed file and what do I compare it to?

      The "checksum" is the same as the "CRC" referenced in the FAQ. The FAQ mentions example code in the CPAN distribution that shows how to calculate the CRC.

      IO::Uncompress::Gunzip will check the sum for you if you set the Strict option:
      gunzip 'file.gz', 'file.out', Strict=>1 or die $GunzipError;
        BTW, if you want to test your error-checking code, you can break the stored checksum like this:
        open my $F, '+<', 'file.gz' or die $!; seek $F, -8, 2; my $c = getc($F); seek $F, -8, 2; print $F chr(ord($c) ^ 1);
        Running this a second time will fix the .gz file.
        Thanks that is helpful So how is
        my $retval = gunzip 'file.gz' => 'file.out', Strict=>1 or $GunzipError +;
        different than
        my $retval = gunzip 'file.gz', 'file.out', Strict=>1 or $GunzipError;