I am interested in opening a single data stream to multiple (possibly
gzipped) files, and was wondering if you could do that elegantly in
Ruby. I have some dodgy old Perl code that works, but it isn’t really
elegant/robust/portable:
I am interested in opening a single data stream to multiple (possibly
gzipped) files, and was wondering if you could do that elegantly in
Ruby. I have some dodgy old Perl code that works, but it isn’t really
elegant/robust/portable:
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.
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