Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Extracting a file from .gz archive

by jonnyfolk (Vicar)
on May 14, 2008 at 09:50 UTC ( #686484=perlquestion: print w/ replies, xml ) Need Help??
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)");

Comment on Extracting a file from .gz archive
Download Code
Re: Extracting a file from .gz archive
by marto (Bishop) 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 :-))
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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2015-07-04 17:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls