http://qs321.pair.com?node_id=486298

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

Hello Monks, I feel kinda like lame asking this question but I am at a real loss. I have a .tgz file in /tmp and I need a perl script to extract the contents to disk say also to /tmp. I have been through archive::tar and cant seem to get my groove on. Can someone show me in the simplest form how to get perl to untar anything.tgz in /tmp

Thanks

Retitled by g0n from 'Untaring'.

Replies are listed 'Best First'.
Re: Untaring with perl
by halley (Prior) on Aug 24, 2005 at 17:28 UTC
    The ".tgz" is an abbreviated file extension indicating ".tar.gz" which is a tarball that was subsequently compressed with gzip. (Of course, a filename does not actually prove what is in the file, it could be a spreadsheet and not a tarball at all, for all we know.) According to Archive::Tar, the module WILL deal with this but only if you have IO::Zlib also installed.

    --
    [ e d @ h a l l e y . c c ]

      Its installed, thanks for your reply.
Re: Untaring with perl
by liverpole (Monsignor) on Aug 24, 2005 at 17:24 UTC
    How about just doing ...?
    #!/usr/bin/perl -w my $file = "/tmp/anything.tgz"; system("tar -vxzf $file");
Re: Untaring with perl
by gryphon (Abbot) on Aug 24, 2005 at 17:30 UTC

    Greetings mrbbq,

    This is untested, and I only just now tossed it together, but is this what you're asking for?

    use Archive::Tar; my $tar = Archive::Tar->new; chdir '/tmp' or die $!; $tar->read('anything.tgz', 1, { 'extract' => 1 } );

    I'd rather not have to chdir; instead, I'd like to tell Archive::Tar where to extract the files. How to do that is probably in the POD somewhere.

    gryphon
    Whitepages.com Development Manager (DSMS)
    code('Perl') || die;

      Works perfect, thank you.
      could anything.tgz be a variable?
      $tar->read('$tar_file",1, { 'extract' => 1 });
      I could not get it to work passing a variable...ideas?
      thanks

        Greetings mrbbq,

        You have a single-quote just before $tar_file that doesn't match the double-quote at the end. You should either change the single to a double, or (and better) just drop the quotes.

        use strict; my $tar_file = 'anything.tgz'; $tar->read( $tar_file, 1, { 'extract' => 1 } );

        gryphon
        Whitepages.com Development Manager (DSMS)
        code('Perl') || die;

Re: Untaring with perl
by derby (Abbot) on Aug 24, 2005 at 17:29 UTC

    Hmmm ... straight from the perldocs:

    use Archive::Tar; my $tar = Archive::Tar->new; $tar->read('origin.tgz',1); $tar->extract();

    What seems to be the problem?

    -derby
      Yeah I know...I read it, looked at it couldnt get it to work? Thanks for your reply.
Re: Untaring with perl
by ait (Hermit) on Aug 24, 2005 at 17:40 UTC
    perldoc -f exec
    perldoc -f system
    my favorite: you can execute a shell command by using the backtick assigning the result to a variable:
    my $hoo = `tar xvfz /tmp/foo`;
    (note that in the example you will not get the actual contents of the tar, you will only get the listing that the tar command outputs)

    --
    Alejandro