Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: File integrity checker

by no_slogan (Deacon)
on Aug 24, 2017 at 16:21 UTC ( #1197942=note: print w/replies, xml ) Need Help??


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;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1197942]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2019-05-25 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (152 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!