#!/usr/bin/perl use strict; use IO::File; use Archive::Zip qw/:ERROR_CODES :CONSTANTS/; my $Usage = "Usage: $0 file.zip\n"; die $Usage unless ( @ARGV == 1 and -f $ARGV[0] ); my $zip = Archive::Zip->new(); if ( $zip->read( $ARGV[0] ) != AZ_OK ) { die "Archive::Zip failed to read $ARGV[0]\n"; } for my $zfile ( $zip->members ) { my $fh = IO::File->new_tmpfile or die "Unable to create temp file: $!\n"; $fh->binmode; if ( $zfile->extractToFileHandle( $fh ) != AZ_OK ) { warn sprintf( "Extraction failed for %s\n", $zfile->fileName() + ); next; } seek( $fh, 0, 0 ); my ( $buffer, $nread ); my $crc = 0; while ( $nread = $fh->read( $buffer, 32768 )) { $crc = Archive::Zip::computeCRC32( $buffer, $crc ); } my $status = ( $crc == $zfile->crc32()) ? "good" : "broke"; printf( "Status: %s\tcomp crc: %08x\tfile crc: %08x\t%s\n", $status, $crc, $zfile->crc32(), $zfile->fileName()); }
UPDATE: I wonder if the problem in the OP code might be here:
It looks like you are extracting the file twice to the same file handle, and maybe that is causing each file to have its contents doubled (and you would almost never get the same crc in that case). In fact, I just added a second "extractToFileHandle" call in my version, and all the data files failed the crc check.$member->extractToFileHandle($fh); if ($member->extractToFileHandle($fh) != 0){ print "Error in $archivedir$filename\n"; next; }
In reply to Re: Zip file CRC validation
by graff
in thread Zip file CRC validation
by msalerno
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |