EvanCarroll has asked for the wisdom of the Perl Monks concerning the following question:

Long time no see monks! Ok, I'm back and this time I'm more powerful than an ancient who just got force-descended by the full-ascended; and, I've got a galactic problem.

I can't figure out why Arhive::Zip is broken, here is a test case, someone fix this for me, and I will grace you all with more divine knowledge about the mighty power of tabs, and the evilness of the dark spaces horde.

Picture here

Note, this is my first perl test, if anyone excessively bitches I will write lots of code and not make any more tests, khtnksxzs

Problem found

Archive::Zip isn't $\ safe, #!/usr/bin/perl -l threw it off.

#!/usr/bin/perl -l use strict; use warnings; use Archive::Zip qw( :ERROR_CODES ); use Digest::MD5; use Test::More tests => 5; use Carp; use constant ERROR_PRONE_FILE => 'error.jpg'; use constant ERROR_FILE => 'corrupterror.jpg'; use constant ERROR_ARCHIVE => 'out.zip'; my $zip = Archive::Zip->new(); my ( $before, $after ); ## ## OPEN GOOD FILE GET MD5 ## { my $md5 = Digest::MD5->new; open ( my $fh, '<', ERROR_PRONE_FILE ) or die 'Can not open file' ; binmode( $fh ); $before = $md5->addfile( $fh )->md5_hex; close $fh; } ## ## Zip up and write out ## { my $md5 = Digest::MD5->new; $zip->addFile( ERROR_PRONE_FILE ); $zip->extractMember( ERROR_PRONE_FILE, ERROR_FILE ); open ( my $fh, '<', ERROR_FILE ) or die 'Can not open file' ; binmode( $fh ); $after = $md5->addfile( $fh )->md5_hex; close $fh; unlink ERROR_FILE; cmp_ok( $after, 'eq', $before , 'Failed test, pre-zip, and post-pre-zip do not match' ); my $status = $zip->writeToFileNamed( ERROR_ARCHIVE ); cmp_ok( $status, '==', AZ_OK, 'Wrote zip out alright' ); } ## ## Read back ## { my $md5 = Digest::MD5->new; my $zip2; eval { $zip2 = Archive::Zip->new( ERROR_ARCHIVE ); }; ok( !$@, "This stupid bugger crashed again, error:\n$@" ); ok ( defined $zip2, 'Perl failed at spawning the object of doom' ) +; $zip2->extractMember( ERROR_PRONE_FILE, ERROR_FILE ); open ( my $fh, '<', ERROR_FILE ) or croak 'Can not open file' ; binmode( $fh ); $after = $md5->addfile( $fh )->md5_hex; close $fh; unlink ERROR_FILE; unlink ERROR_ARCHIVE; cmp_ok( $after, 'eq', $before , 'Failed test, pre-zip, and post-zip do not match' ); } print "Success sums match up all th way through!! Congrats"; print "Verbose: [ $before eq $after ]"; 1;


Evan Carroll
www.EvanCarroll.com

Replies are listed 'Best First'.
Re: .o0O [(i^% Archive::Zip test %^i)]O0o.
by jwkrahn (Abbot) on Mar 30, 2007 at 03:18 UTC
    Your problem is that you are using:
    $after = $md5->addfile( $fh )->md5_hex;
    When you should be using:
    $after = $md5->addfile( $fh )->hexdigest;
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: .o0O [(i^% Archive::Zip test %^i)]O0o.
by diotalevi (Canon) on Mar 30, 2007 at 00:30 UTC

    You probably need to binmode those filehandles you're opening. If you're on Windows and don't use binmode or specify the PerlIO modes somehow, the sequence \r\n is translated to \n on input and the reverse on output. This corrupts binary things like jpegs and zips.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

    A reply falls below the community's threshold of quality. You may see it by logging in.
A reply falls below the community's threshold of quality. You may see it by logging in.