From: Robert Klemme Date: 2005-11-15T00:42:16+09:00 Subject: Re: trouble reading a gzipped xml-file Guido de Melo wrote: > Robert Klemme wrote: >> You're not closing f here. >> > [...] >> >> I'm not so sure that it fails just because of this. > > It shouldn't, because ruby will determine the end of the file by > itself and close the handle on exiting. I didn't mean to say that it fails because of the open file. My point with the first remark was that it's a good habit to open files for only as long as they are actually used. The block form is the idiom of choice here: it's not much longer as a simple File.open() or File.new() and it ensures the file is always properly closed. >> Did you try this? >> >> xmldoc = Zlib::GzipReader.open('file.dia') {|gz| REXML::Document.new >> gz} > > irb(main):003:0> xmldoc = Zlib::GzipReader.open('file.dia') {|gz| > REXML::Document.new gz} > RuntimeError: Zlib::GzipReader is not a valid input stream. It must > be either a String, IO, StringIO or Source. > > This doesn't work either, I'm afraid... I guess this is because GzipReader doesn't inherit IO: >> Zlib::GzipReader.ancestors => [Zlib::GzipReader, Enumerable, Zlib::GzipFile, Object, Kernel] But you can do this xmldoc = Zlib::GzipReader.open('file.dia') {|gz| REXML::Document.new( gz.read )} Btw, you see differing output because you use different printing methods: Zlib::GzipReader.open('file.dia') {|gz| print gz.read } vs. p s If there is no exception during GZIP reading I guess there might be a bug somewhere. As a test I'd write the gunzipped content to another file and do a diff on the plain xml and this output to see whether GzipReader actually yields the same content. Btw, did you actually try to make REXML read the first variant? Maybe you have a problem in your XML file. Kind regards robert