From: Martin Hansen on 18 May 2010 02:11 > Maybe you could reimplement the Perl code trying to decompress always > and trapping the exception when the file is not compressed? Something > like this: > > irb1.9 -rfacets/zlib -rzlib -rstringio > irb(main):001:0> Zlib.decompress("plain text") > Zlib::GzipFile::Error: not in gzip format That is a good idea (I don't think you can rescue exceptions in Perl). However, I am still uncertain if wise/possible and how to get a single file handle on a stream from multiple files. I need to do zipping/unzipping on the fly since I want to work on huge files. Martin -- Posted via http://www.ruby-forum.com/.
From: F. Senault on 18 May 2010 07:43 Le 18 mai � 08:11, Martin Hansen a �crit : >> Maybe you could reimplement the Perl code trying to decompress always >> and trapping the exception when the file is not compressed? Something >> like this: >> >> irb1.9 -rfacets/zlib -rzlib -rstringio >> irb(main):001:0> Zlib.decompress("plain text") >> Zlib::GzipFile::Error: not in gzip format > > That is a good idea (I don't think you can rescue exceptions in Perl). > However, I am still uncertain if wise/possible and how to get a single > file handle on a stream from multiple files. I need to do > zipping/unzipping on the fly since I want to work on huge files. This should work : require "zlib" def openf(fn) File.open(fn) do |f| sig = f.read(3) f.rewind if sig.bytes.to_a == [31, 139, 8] # GZip file signature gz = Zlib::GzipReader.new(f) yield gz gz.close rescue nil else yield f end end end >> openf("toto.gz") { |f| puts f.read } aljezalkezajlkezaj => nil >> openf("toto2.txt") { |f| puts f.read } iappoeipozaipza => nil (You'd want to work on the exception handling etc, of course.) Fred -- Although I may flirt With all kinds of dirt To the point of disease Now I want release From all this decay Take it away And somewhere There's someone who cares With a heart of gold To have and to hold (Depeche Mode, To Have and to Hold)
|
Pages: 1 Prev: Handling Events - Help Next: Dynamically adding methods to a class |