koninka
koninka

Reputation: 53

How to silence the warning in the Archive::Zip

I do not want to see the following messages when extracting archive.

format error: file is too short at /usr/share/perl5/Archive/Zip/Archive.pm line 667.    
Archive::Zip::Archive::_findEndOfCentralDirectory('Archive::Zip::Archive=HASH(0x8acf47c)', 'IO::File=GLOB(0x8ac9d70)') called at /usr/share/perl5/Archive/Zip/Archive.pm line 581
Archive::Zip::Archive::readFromFileHandle('Archive::Zip::Archive=HASH(0x8acf47c)', 'IO::File=GLOB(0x8ac9d70)', 'some zip file') called at /usr/share/perl5/Archive/Zip/Archive.pm line 548

I want to skip some corrupted archives.

Upvotes: 2

Views: 925

Answers (3)

Borodin
Borodin

Reputation: 126742

It is very untidy to mess with the SIGWARN handler or redirect the STDERR output to the null device.

Archive::Zip has a function to set the error handler Archive::Zip::setErrorHandler. The default is to use Carp::carp, which produces the output you're seeing.

You can write your own replacement, for instance to save the error messages in an array for later examination, like this

my @errors;
Archive::Zip::setErrorHandler(sub { push @errors, $_[0] });

Also don't forget to check the return codes from the functions as there will otherwise be no way to know that there has been a problem.

Upvotes: 5

GoinOff
GoinOff

Reputation: 1802

`command-to-extract-file 2>/dev/null`

Pipes stderr to /dev/null

This works for linux. Not sure about windows??

Upvotes: -1

Hunter McMillen
Hunter McMillen

Reputation: 61540

Depending on where you are calling the function that emits this error, you can override the warning handler locally to ignore the warnings.

sub f {
   ...
   local $SIG{__WARN__} = sub { # do nothing };
} 

Now all warnings emitted in function f get routed to your local warn handler, which does nothing.

You should note that Archive::Zip has an error flag AZ_OK that is returned from most operations. For instance:

# Read a Zip file
my $somezip = Archive::Zip->new();
if ( $somezip->read( 'someZip.zip' ) != AZ_OK ) {
    die 'read error';
}

Upvotes: 2

Related Questions