in reply to Checking if a file is present in a ZIP archive

Your decision to check for a signature is a reasonable one.

It is fairly common to take the approach of doing something in an eval block, so that, if it fails for any reason, you will be able to detect that it has done so.   (The built-in variable “$@” will have a value...)

This is actually a very good way to deal with these kinds of errors:

eval { ... something that might fail ... ... (if we made it this far, we didn't fail, so) check return-code } if ($@) { ... guess what, it failed ... } else { ... it didn't ... }

A lot of code these days is intentionally designed in such a way that, if it encounters an exceptional condition that is “plausible and reasonable,” it returns an error-code, but if it encounters a “this can’t possibly ever ever ever happen, but it just did” condition, it throws an exception via die or Carp.   Maybe the designers decided that, if they are supposed to be getting a valid ZIP-file but instead they have a corrupted one (or a flat file containing the text of the Gettysburg Address...), it would be best to commit suicide.   And I would not say that they were wrong.

Replies are listed 'Best First'.
Re^2: Checking if a file is present in a ZIP archive
by bontchev (Sexton) on Mar 25, 2011 at 20:47 UTC

    Yes, I did try trapping the error with eval but it didn't work - it still spews a bunch of errors. I haven't used eval before, so maybe I've messed something up; I'll try again.

    But if the designers have decided that the archive-processing functions should be passed only valid ZIP files, then they should have also provided a function that lets the user test if a file is a valid ZIP file. For instance, the Archive::Extract package does have such a function, but I can't use that package for various reasons (it doesn't have a function for testing the presence of a file with a specified name in the archive and it requires the presence of an unarchiving program).

      I just tried eval also (you need a semicolon after the block following eval I found out) and it was the same.

      I also tried 'no warnings;' for the block containing the read($file) and that didn't help. I have always checked the file extensions before opening something but someone could clobber my file.

      I'm curious now to look under the hood of Archive::Zip even though I probably won't understand it.

        It is probably a wrapper for a library (ZLib?) that might be spewing to STDERR. I hate it when libraries do that.
        Hi, I am using Archive::Zip for compressing files. I am struggling to zip a file with the name contain "ÅÄÖ" characters. Actually these characters are converted as some different symbol. Can anyone give any suggestion to zip a file with the name contain the characters "ÅÄÖ"? Thanks in advance.