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

Hi Monks,

Can anyone advise me on the best way to deal with uploaded files that may be archived and/or compressed?

At the moment, I'm relying on the file extension and just unpacking by backticking the appropriate cmd. I've also had a look at the unix 'file' command, which would let me deal with files with stupid names, but I've no idea if this is a sensible approach.

I know about Archive::Tar and Archive::Zip, but what I really want is Archive::AnythingYouLike (within reason). Besides, I don't care about manipulating the archive, I just want it unpacked so maybe any of the Archive modules are overkill?

Help?

Cheers, Cxx

Replies are listed 'Best First'.
Re: archives etc
by coec (Chaplain) on Apr 21, 2004 at 12:18 UTC
    How about using the standard Unix utility 'file' or even the Perl module File::Type.

    use warnings; use strict; my @Progs = ('gzip', 'bzip2'); my $File = <STDIN>; foreach (@Progs) { my $Output = `file $File`; chomp $Output; `$_ -d $File` if ($Output =~ $_); }
    I tried to have a look at File::Type also but during the install it spat lots of errors so I didn't go any further.

    CC

Re: archives etc
by DrHyde (Prior) on Apr 21, 2004 at 11:21 UTC
    I expect you want a variation on a theme of ...
    if(system('gzip', '-d', $file) == 0) { # it was a gzipped file } elsif(system('bzip2', '-d', $file) == 0) { # it was a bzipped file } ...
    This relies on the standard Unixish behaviour of applications exiting with status zero if successful, non-zero otherwise. No doubt you could also do something similar with open()ing a pipe to gzip/bzip2/etc and if the open() fails, try the next one on your list.
Re: archives etc
by matija (Priest) on Apr 21, 2004 at 11:31 UTC
    I know it's not what you asked, but instead of backticks, I like this method:
    foreach (@ARGV) { if ($_=~/\.z$/) { $_="gunzip -c $_ |"; } }
    Putting that in front of your main loop means your while (<ARGV>) loop can process gzip files transparently.
      .z? You actually remember the times when this was the default for gzipped files? :-)
      ooh, clever. But that only works if I'm opening the files in perl? What I want to do is unpack them into a directory for R (stats package) and as far as I know there's no way of passing a perl file directly to an R script. So it won't work in my case? Or have I misunderstood what it's doing?

      Cxx