# Re: Archive::Tar uncompress question

**URL:** <https://www.ruby-forum.com/t/re-archive-tar-uncompress-question/93705>\
**Category:** Ruby\
**Created:** [April 6, 2006, 10:05pm UTC](https://www.ruby-forum.com/t/re-archive-tar-uncompress-question/93705 "2006-04-06T22:05:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [April 6, 2006, 10:05pm UTC](https://www.ruby-forum.com/t/re-archive-tar-uncompress-question/93705/1 "2006-04-06T22:05:17Z")

</div>

> guess was an unforseen use. I need to simply unpack an  
> when /.t(ar.)?gz/  
> which works fine for what I need _if_ I redefine
> 
> It seems that @compressed\_archive\_name can only be set if one  
> first compresses an archive. I don’t need to do that here -  
> I just need to uncompress an already-existing archive.
> 
> Should I be redefining this method or should the package be  
> changed to the (my) expected behavior? Or have I just  
> totally missed the usage here? Has anyone else run up against this?
> 
> - Dimitri

Hi Dimitri,

It basically boils down to a case of unexpected usage. The assumption  
is that you’re creating (or going to create) an archive when you use  
Archive::Tar.new.

You are, of course, free to redefine uncompress\_archive, but rather than  
patching the lib itself, I recommend taking advantage of Ruby’s open  
classes:

# Somewhere in your code:

module Archive  
class Tar  
def uncompress\_archive(program=“gunzip”)  
@compressed\_archive\_name = @archive\_name unless  
@compressed\_archive\_name

```
     cmd = "#{program} #{@compressed_archive_name}"

     Open3.popen3(cmd){ |prog_in, prog_out, prog_err|
        err = prog_err.gets
        if err
           raise CompressError, err.chomp
        end
        @compressed_archive_name = nil
     }
     self
  end

```

end  
end

I suppose I could add class methods that would do the sort of thing  
you’re trying to do. Or, you could add your own class method.

Also note that I’ll probably be changing the name to  
Archive::Tar::External in the future.

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [April 6, 2006, 10:32pm UTC](https://www.ruby-forum.com/t/re-archive-tar-uncompress-question/93705/2 "2006-04-06T22:32:49Z")

</div>

Hi Dan,

On 4/6/06, Berger, Daniel [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It basically boils down to a case of unexpected usage. The assumption  
> is that you’re creating (or going to create) an archive when you use  
> Archive::Tar.new.

I actually chose Archive::Tar over Archive::Tar::Minitar because I  
needed something quick-and-dirty that could handle all the kinds of  
archives in my original post. My application only deals with the  
un-packing of these archives - the originals stay where they are, and  
my app doesn’t create them.

> You are, of course, free to redefine uncompress\_archive, but rather than  
> patching the lib itself, I recommend taking advantage of Ruby’s open  
> classes:
> 
> # Somewhere in your code:

This is actually what I have done. I just presented it in  
pseudo-patch form for conciseness, and perhaps to convince you that it  
really isn’t an error, but could be handled differently. 🙂

> I suppose I could add class methods that would do the sort of thing  
> you’re trying to do. Or, you could add your own class method.

A la

module Archive  
class Tar  
attr\_accessor :compressed\_archive\_name  
end  
end

?

That still involves redefinition. I try to avoid that with  
non-standard-lib libraries. You never know when the name is going to  
change. 🙂

> Also note that I’ll probably be changing the name to  
> Archive::Tar::External in the future.

Ok, thanks. Interesting naming discussion this started…

- Dimitri

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [April 6, 2006, 11:21pm UTC](https://www.ruby-forum.com/t/re-archive-tar-uncompress-question/93705/3 "2006-04-06T23:21:27Z")

</div>

Dimitri A. wrote:

> archives in my original post. My application only deals with the
> 
> class Tar  
> attr\_accessor :compressed\_archive\_name  
> end  
> end
> 
> ?
> 
> That still involves redefinition. I try to avoid that with  
> non-standard-lib libraries. You never know when the name is going to  
> change. 🙂

I thought about that, too. I don’t see the harm, really. If you want  
to  
uncompress an existing archive then it would just be a matter of  
assigning to  
that attribute first, then uncompressing.

What makes more sense and/or which would you prefer?

t = Tar::External.new  
t.compressed\_archive\_name = ‘somefile.tar.gz’  
t.uncompress

# or

Tar::External.uncompress(‘somefile.tar.gz’)

The advantage to the latter is that it’s simpler. The drawback is that  
it  
wouldn’t return an object.

Come to think of it, I should probably manually define  
compressed\_archive\_name=  
like so in order to ensure other instance variables are set properly:

How about this patch:

# Assign a compressed archive name. This autogenerates the archive\_name

# based on the extension of the name provided, unless you provide the

# extension yourself. If the extension is ‘.tgz’, then the base of the

# name + ‘.tar’ will be the new archive name.

# 

# This should only be used if you have a pre-existing, compressed

archive

# that you want to uncompress.

def compressed\_archive\_name=(name, ext=File.extname(name))  
if ext == “.tgz” || ext == ‘.TGZ’  
@archive\_name = File.basename(name, ext) + ‘.tar’  
else  
@archive\_name = File.basename(name, ext)  
end  
@compressed\_archive\_name = name  
end

Thoughts?

Dan

PS - Perhaps we should continue this over on the Shards project page  
under  
“Open Discussion”. See [http://www.rubyforge.org/projects/shards](http://www.rubyforge.org/projects/shards).

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [April 7, 2006, 6:51pm UTC](https://www.ruby-forum.com/t/re-archive-tar-uncompress-question/93705/4 "2006-04-07T18:51:26Z")

</div>

Dimitri A. wrote:

> On 4/6/06, Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> - Dimitri  
> Ok, cool. I’ve renamed everything and put a new release out.

Thanks for the feedback.

Dan

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [April 7, 2006, 9:57pm UTC](https://www.ruby-forum.com/t/re-archive-tar-uncompress-question/93705/5 "2006-04-07T21:57:50Z")

</div>

On 4/7/06, Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ok, cool. I’ve renamed everything and put a new release out.

Great! Thanks, Dan. I’ll put those new methods to work on Monday.

- Dimitri

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [April 7, 2006, 8:38am UTC](https://www.ruby-forum.com/t/re-archive-tar-uncompress-question/93705/6 "2006-04-07T08:38:00Z")

</div>

On 4/6/06, Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The advantage to the latter is that it’s simpler. The drawback is that it  
> wouldn’t return an object.

I prefer the latter. Would you really need an object returned if  
you’re just uncompressing an archive?

> Come to think of it, I should probably manually define compressed\_archive\_name=  
> like so in order to ensure other instance variables are set properly:
> 
> How about this patch:

> Thoughts?

Looks good. I would still change the “unless” clause in the  
uncompress\_archive method, in addition to this patch. Then both  
methods of uncompressing could be supported.

> PS - Perhaps we should continue this over on the Shards project page under  
> “Open Discussion”. See [http://www.rubyforge.org/projects/shards](http://www.rubyforge.org/projects/shards).

I’m happy with where this thread has ended up. If you feel more  
discussion is necessary, let me know, and I’ll follow you over there.

- Dimitri
