I have a .gz file that contains an XML document. Does anyone know how to use Zlib properly? So far, I have the following code:
require 'zlib'
Zlib::GzipReader.open('PRIDE_Exp_Complete_Ac_1015.xml.gz') { |gz|
g = File.new("PRIDE_Exp_Complete_Ac_1015.xml", "w")
g.write(gz)
g.close()
}
But this creates a blank .xml document. Does anyone know how I can properly do this?
Zlib::GzipReaderworks like mostIO-like classes do in Ruby. You have anopencall, and when you pass a block to it, the block will receive theIO-like object. Think of it is convenient way of doing something with a file or resource for the duration of the block.But that means that in your example
gzis anIO-like object, and not actually the contents of the gzip file, as you expect. You still need toreadfrom it to get to that. The simplest fix would then be:Note that this will read the entire contents of the uncompressed gzip into memory.
If all you’re really doing is copying from one file to another, you can use the more efficient
IO.copy_streammethod. Your example might then look like:Behind the scenes, this will try to use the
sendfilesyscall available in some specific situations on Linux. Otherwise, it will do the copying in fast C code 16KB blocks at a time. This I learned from the Ruby 1.9.1 source code.