From: "Berger, Daniel" Date: 2006-04-07T05:03:24+09:00 Subject: Re: Archive::Tar uncompress question > -----Original Message----- > From: Dimitri Aivaliotis [mailto:aglarond@gmail.com] > Sent: Thursday, April 06, 2006 1:06 PM > To: ruby-talk ML > Subject: Archive::Tar uncompress question > > > Hi, > > I needed to use Archive::Tar today, and was bit by what I > guess was an unforseen use. I need to simply unpack an > archive, optionally decompressing it first. So, I have code > like this: > > def expand > t = Archive::Tar.new( @name ) > case @name > when /\.tar\.bz2/ > t.expand > when /\.bz2/ > t.uncompress("bunzip2") > when /\.t(ar\.)?gz/ > t.expand > when /\.gz/ > t.uncompress("gunzip") > when /\.zip/ > t.uncompress("unzip") > else > raise UnknownArchiveError > end > end > > which works fine for what I need *if* I redefine > uncompress_archive like so: > > > - unless @compressed_archive_name > - raise CompressError, "no compressed file found" > - end > + unless @compressed_archive_name > + @compressed_archive_name = @archive_name > + end > > > 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