Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

testing for a zip file

by Anonymous Monk
on Aug 29, 2003 at 14:21 UTC ( #287666=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone know how to test a file to see if it's a zipped file or not? Many thanks.

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

    Check out File::Type

    #!/usr/bin/perl -w use strict; use File::Type; my $filename = 'myfile.zip'; my $filetype = File::Type->new(); my $type = $filetype->checktype_filename($filename); my $zipped = ($type eq 'application/zip'); print "$filename is of type $type (", $zipped ? '' : 'not ', "zipped)\ +n";

    When you say "zipped" do you mean "compressed" or do you mean that a specific compression algorithm/tool was used (i.e., Windows PKZIP format)?

    File::Type can also tell you if the file is compressed in other ways, though you'll have to check the return value for each type.

    -- Eric Hammond

Re: testing for a zip file
by asarih (Hermit) on Aug 29, 2003 at 14:35 UTC
    You can (in no particular order)
    1. call qx[ file $file ] and grep if the output says it's a ZIP file
    2. read the first few bytes and see if they match the string PK\003\004
    3. use Archive::Zip (this may or may not help)
Re: testing for a zip file
by tcf22 (Priest) on Aug 29, 2003 at 14:25 UTC
    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.

      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.
      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.

Re: testing for a zip file
by dtr (Scribe) on Aug 29, 2003 at 15:51 UTC

    You need the mime magic library (have a look at /etc/mime-magic on linux). The File (1) command uses this, and a quick search on CPAN reveals a File::MMagic module which purports to do something similar.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://287666]
Approved by tcf22
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2022-12-05 08:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?