http://www.perlmonks.org?node_id=686484

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

I am downloading a file via a remote script, and I need to decompress it, which I've never done before. The printout reports no errors but neither does it create the file! I am not clear on what I should be doing here!
#!/usr/bin/perl -w use strict; use CGI ':standard'; use Archive::Tar; my $file = "/home/mm/IpToCountry.csv.gz"; my $end_file = "/home/mm/IpToCountry.csv"; print header(); my $tar = Archive::Tar->new; my $tar_errors = $tar->error(); $tar->read($file,1); $tar->extract($file, $end_file); print p("Tar errors ($tar_errors)");

Replies are listed 'Best First'.
Re: Extracting a file from .gz archive
by mwah (Hermit) on May 14, 2008 at 10:28 UTC

    There's no .tar involved !?

    BTW: in addition to the valueable links already given, you could simply invoke the Zlib-module from CPAN:

    ... use IO::Zlib; my $file = "IpToCountry.csv.gz"; my $end_file = "IpToCountry.csv"; my $fh = IO::Zlib->new($file, 'rb'); if(defined $fh) { open my $fd, '>', $end_file or die "$end_file $!"; while(my $line = <$fh>) { print $fd $line } undef $fh; } ...

    This will extract the .csv.gz to a .csv

    Regards

    mwa

      This solution did the job perfectly. Thank you for your help.
Re: Extracting a file from .gz archive
by marto (Cardinal) on May 14, 2008 at 10:08 UTC
      Hi Martin ,

      Archive::Tar supports gzip'ed files iff the IO::Zlib module is installed - or so its' POD claims and I've no reason to question it:-)

      At last, a user level that best describes my experience :-))
Re: Extracting a file from .gz archive
by Bloodnok (Vicar) on May 14, 2008 at 10:11 UTC
    Hi ,

    If you want to extract a file to a path, use the

    extract_file( $file, [$extract_path] )
    method. Also, you probably want to call the error() method after the read() and extract_file() calls.

    For further info perldoc Archive::Tar ...

    HTH

    At last, a user level that best describes my experience :-))