Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

How to find out a compressed file inside a script ?

by balakrishnan (Monk)
on Feb 19, 2009 at 15:26 UTC ( #745089=perlquestion: print w/ replies, xml ) Need Help??
balakrishnan has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I written a script, that will get a text file as command line arguments. Sometimes, it can get compressed text file as an argument.
Is there way to find out that the input file is compressed one ?

I dont want to use the regular expression to check the extension of the file for finding out.

Comment on How to find out a compressed file inside a script ?
Re: How to find out a compressed file inside a script ?
by moritz (Cardinal) on Feb 19, 2009 at 15:34 UTC
    You could check for a magic number, for example by using File::MMagic or similar modules. Or you could just try to decode it, and treat it as uncompressed if the decoding fails.
      ...or, if on *NIX, first use magic(1) - the compression method just might be in /etc/magic ... you never know:-)

      A user level that continues to overstate my experience :-))
Re: How to find out a compressed file inside a script ?
by prasadbabu (Prior) on Feb 19, 2009 at 15:35 UTC

    Hi balakrishnan,

    try the below code and add your extensions as per your requirement, $filename =~ /\.(zip|gzip|txt)$/i

    Also you can use File::Basename to get the file extensions exactly.

    update: Sorry I misread the question, forgot to see 'dont'

    Prasad

Re: How to find out a compressed file inside a script ?
by dwm042 (Priest) on Feb 19, 2009 at 17:08 UTC
    On the Unix command line, I tend to use 'file':

    file *.zip applepie.zip: ZIP archive keylime.zip: ZIP archive

    Which is pretty much the same technique (relying on a magic number) as moritz and Bloodnok are advocating.
Re: How to find out a compressed file inside a script ?
by lakshmananindia (Chaplain) on Feb 20, 2009 at 04:43 UTC

    Once I got a chance to read Archive::Tar module. There is a function named _read_tar. In that function they will read first 512 bytes from the file. They will check whether it contains the GZIP magic number is there in the 512 bytes.

    The magic number is a constant defined in Constant.pm inside the Tar directory

    I suggest you to go through that module. I feel this information can help you

Re: How to find out a compressed file inside a script ?
by balakrishnan (Monk) on Feb 20, 2009 at 10:49 UTC


    UPDATE : Finally i got it by,
    perl -MFile::Type -le 'print File::Type->new->checktype_filename("text +.gz")'

    Thanks to the monks.


Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2014-07-14 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (254 votes), past polls