• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 448
  • Last Modified:

capture corrupt zip file error from system unzip in perl script

I have a piece of perl code which runs a system unzip command to unzip a file, and sometimes the zip file is corrupt, and I want to be able to capture that error to force the script to die at that point.  Here's what my code looks like:

print "unzip -oP $PassWord $ZipFile -d $DestDir";
system("unzip -oP $PassWord $ZipFile -d $DestDir");
        if($!) {
                die "Cannot unzip $ZipFile to $DestDir: $!";
                return 0;
        } else {
                return 1;

I use a corrupt zip file for testing, and when I run it through the Perl debugger, I get this when it tries to unzip:

End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.
unzip:  cannot find zipfile directory in one of /nfs/AutoloadShare/Vendors/Ford/navis/Datafiles/Full2/navisfull.zip or
        /nfs/AutoloadShare/Vendors/Ford/navis/Datafiles/Full2/navisfull.zip.zip, and cannot find /nfs/AutoloadShare/Vendors/Ford/navis/Datafiles/Full2/navisfull.zip.ZIP, period.

So that's good, I have the error I expect.  But the problem is, I can't capture the error code from this.  I looked at $!, and it is undefined.  I also looked at $?, and it has a number, but when I run $?&127, it returns 0.  And unless $! is set, it returns true and the rest of the code executes without error.  I want to stop execution and die with error if it has this error from a corrupt file.  What do I need to check for in my if statement to accomplish this?
1 Solution
You can't use system to capture the output.
my $output = qx(unzip -oP $PassWord $ZipFile -d $DestDir 2>&1);

die "Cannot unzip $ZipFile to $DestDir: $output\n";
return 1;

Open in new window


Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now