From: Brian Candler Date: 2010-08-18T18:40:25+09:00 Subject: Re: Making File.open work on gzipped files Robert Klemme wrote: > On 17.08.2010 20:56, Martin Hansen wrote: >> Thanks Brian, >> >> I did look at ruby's zlib and wondered why there is no method to check >> if a file is zipped or not - one could perhaps could fix something by >> rescuing the Zlib exception? > > Exactly, just try to open with GzipReader and if that throws just work > with the regular file which you have opened already. Remember to rewind it too. >> require 'zlib' => true >> io = File.open("/etc/passwd") => # >> z = Zlib::GzipReader.new(io) Zlib::GzipFile::Error: not in gzip format from (irb):3:in `initialize' from (irb):3:in `new' from (irb):3 from :0 >> io.gets => "1:119:PulseAudio daemon,,,:/var/run/pulse:/bin/false\n" >> io.rewind => 0 >> io.gets => "root:x:0:0:root:/root:/bin/bash\n" -- Posted via http://www.ruby-forum.com/.