Repost: minitar - how to extract only one file from a tar archive as an object

trying again....

:wink:

SM

路路路

---------- Forwarded message ----------
From: Simon Mullis <simon@mullis.co.uk>
Date: Sep 27, 2007 4:16 PM
Subject: minitar - how to extract only one file from a tar archive as an object
To: ruby-talk@ruby-lang.org

Ahoy there, fellow rubyphiles...

I'm trying to use Archive::Tar::Minitar to extract a single file from
a tar archive:

"test.tar" contains:

./test/foo
./test/bar
./test/baz

<code>
# lifted from the minitar source...
require 'archive/tar/minitar'
include Archive::Tar

a = File.open("test.tar", "rb")
Minitar::Input.open(a) do |inp|
聽聽聽聽inp.each do |entry|
聽聽聽聽聽聽聽inp.extract_entry(".", entry) if entry.full_name =~ /baz/
聽聽聽聽end
end
</code>

This all works fine, and writes to "./test/baz"

But!

How could I do the same and create a File object of the extracted
file? (i.e. not write anything to disk).

(The reason I ask is the file I want is a zip, which lives inside the
tar archive and I then want to grab some files from that using the
'rubyzip' gem.)

It's quite possible (in fact, more than likely) that I've missed
something obvious and there is already a straightforward way to do
this.

To be honest, a few more examples of minitar in use would be great.

Many thanks in advance for any pointers or tips.

Cheers!

--
Simon Mullis
_________________
simon@mullis.co.uk

Hi,

Use entry.read instead of inp.extract_entry; this will not extract the file
to disk, but returns its contents as a string. And if wanted, you can wrap
that string in a StringIO to get a file-like object:

require 'archive/tar/minitar'
include Archive::Tar

require 'stringio'

a = File.open("test.tar", "rb")
Minitar::Input.open(a) do |inp|
聽聽inp.each do |entry|
聽聽聽聽if entry.full_name =~ /baz/
聽聽聽聽聽聽f = StringIO.new( entry.read)
聽聽聽聽聽聽#Do something useful with f
聽聽聽聽end
聽聽end
end

Regards,
Raf

路路路

2007/9/28, Simon Mullis <simon@mullis.co.uk>:

trying again....

:wink:

SM

---------- Forwarded message ----------
From: Simon Mullis <simon@mullis.co.uk>
Date: Sep 27, 2007 4:16 PM
Subject: minitar - how to extract only one file from a tar archive as an
object
To: ruby-talk@ruby-lang.org

Ahoy there, fellow rubyphiles...

I'm trying to use Archive::Tar::Minitar to extract a single file from
a tar archive:

"test.tar" contains:

./test/foo
./test/bar
./test/baz

<code>
# lifted from the minitar source...
require 'archive/tar/minitar'
include Archive::Tar

a = File.open("test.tar", "rb")
Minitar::Input.open(a) do |inp|
聽聽聽聽inp.each do |entry|
聽聽聽聽聽聽聽inp.extract_entry(".", entry) if entry.full_name =~ /baz/
聽聽聽聽end
end
</code>

This all works fine, and writes to "./test/baz"

But!

How could I do the same and create a File object of the extracted
file? (i.e. not write anything to disk).

(The reason I ask is the file I want is a zip, which lives inside the
tar archive and I then want to grab some files from that using the
'rubyzip' gem.)

It's quite possible (in fact, more than likely) that I've missed
something obvious and there is already a straightforward way to do
this.

To be honest, a few more examples of minitar in use would be great.

Many thanks in advance for any pointers or tips.

Cheers!

--
Simon Mullis
_________________
simon@mullis.co.uk

Great! This is exactly what I was looking for!

Thankyou.

SM

路路路

On 9/28/07, Raf Coremans <rrafje@gmail.com> wrote:

2007/9/28, Simon Mullis <simon@mullis.co.uk>:
>
> trying again....
>
> :wink:
>
> SM
>
> ---------- Forwarded message ----------
> From: Simon Mullis <simon@mullis.co.uk>
> Date: Sep 27, 2007 4:16 PM
> Subject: minitar - how to extract only one file from a tar archive as an
> object
> To: ruby-talk@ruby-lang.org
>
>
> Ahoy there, fellow rubyphiles...
>
> I'm trying to use Archive::Tar::Minitar to extract a single file from
> a tar archive:
>
> "test.tar" contains:
>
> ./test/foo
> ./test/bar
> ./test/baz
>
> <code>
> # lifted from the minitar source...
> require 'archive/tar/minitar'
> include Archive::Tar
>
> a = File.open("test.tar", "rb")
> Minitar::Input.open(a) do |inp|
> inp.each do |entry|
> inp.extract_entry(".", entry) if entry.full_name =~ /baz/
> end
> end
> </code>
>
> This all works fine, and writes to "./test/baz"
>
> But!
>
> How could I do the same and create a File object of the extracted
> file? (i.e. not write anything to disk).
>
> (The reason I ask is the file I want is a zip, which lives inside the
> tar archive and I then want to grab some files from that using the
> 'rubyzip' gem.)
>
> It's quite possible (in fact, more than likely) that I've missed
> something obvious and there is already a straightforward way to do
> this.
>
> To be honest, a few more examples of minitar in use would be great.
>
> Many thanks in advance for any pointers or tips.
>
> Cheers!
>
> --
> Simon Mullis
> _________________
> simon@mullis.co.uk
>
>

Hi,

Use entry.read instead of inp.extract_entry; this will not extract the file
to disk, but returns its contents as a string. And if wanted, you can wrap
that string in a StringIO to get a file-like object:

require 'archive/tar/minitar'
include Archive::Tar

require 'stringio'

a = File.open("test.tar", "rb")
Minitar::Input.open(a) do |inp|
聽聽inp.each do |entry|
聽聽聽聽if entry.full_name =~ /baz/
聽聽聽聽聽聽f = StringIO.new( entry.read)
聽聽聽聽聽聽#Do something useful with f
聽聽聽聽end
聽聽end
end

Regards,
Raf

--
Simon Mullis
_________________
simon@mullis.co.uk

Hi

Anyone have any examples of using rubyzip with a stringio object of a
zip (that I've just extracted from elsewhere using Minitar)...

I cannot seem to force it to accept a non-File object.

Anyone offer any inspiration?

Thanks

SM

路路路

On 9/28/07, Simon Mullis <simon@mullis.co.uk> wrote:

Great! This is exactly what I was looking for!

Thankyou.

SM

On 9/28/07, Raf Coremans <rrafje@gmail.com> wrote:
> 2007/9/28, Simon Mullis <simon@mullis.co.uk>:
> >
> > trying again....
> >
> > :wink:
> >
> > SM
> >
> > ---------- Forwarded message ----------
> > From: Simon Mullis <simon@mullis.co.uk>
> > Date: Sep 27, 2007 4:16 PM
> > Subject: minitar - how to extract only one file from a tar archive as an
> > object
> > To: ruby-talk@ruby-lang.org
> >
> >
> > Ahoy there, fellow rubyphiles...
> >
> > I'm trying to use Archive::Tar::Minitar to extract a single file from
> > a tar archive:
> >
> > "test.tar" contains:
> >
> > ./test/foo
> > ./test/bar
> > ./test/baz
> >
> > <code>
> > # lifted from the minitar source...
> > require 'archive/tar/minitar'
> > include Archive::Tar
> >
> > a = File.open("test.tar", "rb")
> > Minitar::Input.open(a) do |inp|
> > inp.each do |entry|
> > inp.extract_entry(".", entry) if entry.full_name =~ /baz/
> > end
> > end
> > </code>
> >
> > This all works fine, and writes to "./test/baz"
> >
> > But!
> >
> > How could I do the same and create a File object of the extracted
> > file? (i.e. not write anything to disk).
> >
> > (The reason I ask is the file I want is a zip, which lives inside the
> > tar archive and I then want to grab some files from that using the
> > 'rubyzip' gem.)
> >
> > It's quite possible (in fact, more than likely) that I've missed
> > something obvious and there is already a straightforward way to do
> > this.
> >
> > To be honest, a few more examples of minitar in use would be great.
> >
> > Many thanks in advance for any pointers or tips.
> >
> > Cheers!
> >
> > --
> > Simon Mullis
> > _________________
> > simon@mullis.co.uk
> >
> >
>
> Hi,
>
>
> Use entry.read instead of inp.extract_entry; this will not extract the file
> to disk, but returns its contents as a string. And if wanted, you can wrap
> that string in a StringIO to get a file-like object:
>
> require 'archive/tar/minitar'
> include Archive::Tar
>
> require 'stringio'
>
> a = File.open("test.tar", "rb")
> Minitar::Input.open(a) do |inp|
> inp.each do |entry|
> if entry.full_name =~ /baz/
> f = StringIO.new( entry.read)
> #Do something useful with f
> end
> end
> end
>
>
> Regards,
> Raf
>

--
Simon Mullis
_________________
simon@mullis.co.uk

--
Simon Mullis
_________________
simon@mullis.co.uk