Zlib::GzipReader
works like most IO
-like classes do in Ruby. You have an open
call, and when you pass a block to it, the block will receive the IO
-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 gz
is an IO
-like object, and not actually the contents of the gzip file, as you expect. You still need to read
from it to get to that. The simplest fix would then be:
g.write(gz.read)
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_stream
method. Your example might then look like:
Zlib::GzipReader.open('PRIDE_Exp_Complete_Ac_1015.xml.gz') do | input_stream |
File.open("PRIDE_Exp_Complete_Ac_1015.xml", "w") do |output_stream|
IO.copy_stream(input_stream, output_stream)
end
end
Behind the scenes, this will try to use the sendfile
syscall 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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…