Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Using Archive::Tar on tar data already in memory

by shmem (Chancellor)
on Oct 16, 2006 at 16:42 UTC ( [id://578549]=note: print w/replies, xml ) Need Help??


in reply to Using Archive::Tar on tar data already in memory

Use IO::Scalar and pass it a reference to your unbzip2'ed data (tested):

#!/usr/bin/perl -w use strict; use Compress::Bzip2; use Archive::Tar; use IO::Scalar; use Data::Dumper; $Data::Dumper::Indent = 1; my $file = shift or die "usage: $0 filename.tar.bz2\n"; my $bz = bzopen($file,"r") or die; my ($out, $buf); $out .= $buf while $bz->bzread($buf) > 0; my $fh = IO::Scalar->new(\$out); my $tar = Archive::Tar->new; $tar->read($fh) or die "Cannot read from \$fh"; my @fileinfo = $tar->list_files([qw(name size)]); print Dumper(\@fileinfo);

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^2: Using Archive::Tar on tar data already in memory
by alpiner2 (Initiate) on Oct 02, 2007 at 15:47 UTC

    Suppose we were to take this one step further and have this completely in memory without reading in a file at all.

    I'm using LWP::Simple and pull down a tar.gz from a cgi that prints the binary data.
    $tarInput is a scalar which contains the tar data. Since gzread (or bzread) can't read in
    scalars or filehandles created from IO::Scalar, need to use a combination of the above 2 posts.
    I pass $tarInput to Compress::Zlib::memGunzip and it returns another scalar with the uncompressed
    data. Take that data and create a filehandle with the IO::Scalar module and pass that to
    $tar->read. I had been working on finding a solution for this and thought this thread would be appropriate
    to post my findings. As always there could be more than one way to do this, this just happens to
    work for me.

    use Archive::Tar; use LWP::Simple; use IO::Scalar; use Compress::Zlib; use strict; my $device = shift; my $tarInput = get "http://somehost/cgi-bin/getTar.cgi?device=$device" +; my $uncomptar = Compress::Zlib::memGunzip($tarInput); my $sh = new IO::Scalar \$uncomptar; my $tar = Archive::Tar->new; $tar->read($sh); print join "\n", $tar->list_files;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://578549]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-25 22:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found