troubleshooting Question

capture corrupt zip file error from system unzip in perl script

Avatar of texasreddog
texasreddog asked on
Perl
1 Comment1 Solution515 ViewsLast Modified:
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?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 1 Comment.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros