in reply to testing for a zip file

You can check the first 2 bytes of the file. If they are PK, then you have a .zip file.
open(F, "file.zip") || die "File open failed: $!\n"; if(read(F, $buffer, 2)){ if($buffer eq 'PK'){ print "We have a zip\n"; }else{ print "No zip here\n"; } } close(F);

Update: This is for Windows/DOS based .ZIP archives only. Not for gzip files.

Replies are listed 'Best First'.
Re: Re: testing for a zip file
by ChrisS (Monk) on Aug 29, 2003 at 14:27 UTC

    Aren't you assuming a specific tool was used to do the zipping, though? Or, is that a universal standard?

    Try this (requires the Archive::Zip module):

    use Archive::Zip; $zip = Archive::Zip->new(); die "Invalid zip file!" if $zip->read("zipfile.zip") != AZ_OK;
    Update:You may need some of the other Archive modules if you want to check for gzipped files.
      As far as I know all Windows/DOS based .ZIP archives start with PK. This however is not the case for gzip(.gz) files.
Re: Re: testing for a zip file
by adrianh (Chancellor) on Aug 29, 2003 at 21:10 UTC
    You can check the first 2 bytes of the file. If they are PK, then you have a .zip file.

    Or, to be slightly more accurate, if they are not PK you know that you don't have a .zip file :-)

    There is nothing to stop a non-zip file starting with PK.

    If you want to be sure use Archive::Zip to attempt to extract the file or check the file's CRCs.