From: Daniel Berger Date: 2006-04-07T06:19:19+09:00 Subject: Re: Archive::Tar uncompress question Dimitri Aivaliotis wrote: > Hi Dan, > > On 4/6/06, Berger, Daniel 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. :) 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.