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

**URL:** <https://www.ruby-forum.com/t/repost-minitar-how-to-extract-only-one-file-from-a-tar-archive-as-an-object/115773>\
**Category:** Ruby\
**Created:** [September 28, 2007, 12:37pm UTC](https://www.ruby-forum.com/t/repost-minitar-how-to-extract-only-one-file-from-a-tar-archive-as-an-object/115773 "2007-09-28T12:37:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijomo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/87869e/32.png) [@sijomo](https://www.ruby-forum.com/u/sijomo)\
**Post date:** [September 28, 2007, 12:37pm UTC](https://www.ruby-forum.com/t/repost-minitar-how-to-extract-only-one-file-from-a-tar-archive-as-an-object/115773/1 "2007-09-28T12:37:06Z")

</div>

trying again…

😉

SM

---------- Forwarded message ----------  
From: Simon M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Date: Sep 27, 2007 4:16 PM  
Subject: minitar - how to extract only one file from a tar archive as an  
object  
To: removed\_email\_address@domain.invalid

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

`
# 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
`

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!

---

<div class="post-metadata">

**Author:** ![sijomo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/87869e/32.png) [@sijomo](https://www.ruby-forum.com/u/sijomo)\
**Post date:** [September 28, 2007, 1:58pm UTC](https://www.ruby-forum.com/t/repost-minitar-how-to-extract-only-one-file-from-a-tar-archive-as-an-object/115773/2 "2007-09-28T13:58:32Z")

</div>

2007/9/28, Simon M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Subject: minitar - how to extract only one file from a tar archive as an
> 
> Minitar::Input.open(a) do |inp|  
> How could I do the same and create a File object of the extracted  
> To be honest, a few more examples of minitar in use would be great.

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

---

<div class="post-metadata">

**Author:** ![sijomo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/87869e/32.png) [@sijomo](https://www.ruby-forum.com/u/sijomo)\
**Post date:** [September 28, 2007, 2:55pm UTC](https://www.ruby-forum.com/t/repost-minitar-how-to-extract-only-one-file-from-a-tar-archive-as-an-object/115773/3 "2007-09-28T14:55:55Z")

</div>

Great! This is exactly what I was looking for!

Thankyou.

SM

---

<div class="post-metadata">

**Author:** ![sijomo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/87869e/32.png) [@sijomo](https://www.ruby-forum.com/u/sijomo)\
**Post date:** [September 28, 2007, 4:45pm UTC](https://www.ruby-forum.com/t/repost-minitar-how-to-extract-only-one-file-from-a-tar-archive-as-an-object/115773/4 "2007-09-28T16:45:32Z")

</div>

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
