capture corrupt zip file error from system unzip in perl script

Posted on 2009-12-22
Last Modified: 2012-05-08
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/ or
        /nfs/AutoloadShare/Vendors/Ford/navis/Datafiles/Full2/, and cannot find /nfs/AutoloadShare/Vendors/Ford/navis/Datafiles/Full2/, 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?
Question by:texasreddog
    1 Comment
    LVL 48

    Accepted 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

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
    A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now