Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
class method
Zlib::GzipReader.zcat
Zlib::GzipReader.zcat(io, options = {}, &block) => nil
Zlib::GzipReader.zcat(io, options = {}) => string
ext/zlib/zlib.c
View on GitHub ↗
static VALUE
rb_gzreader_s_zcat(int argc, VALUE *argv, VALUE klass)
{
VALUE io, unused, obj, buf=0, tmpbuf;
long pos;
rb_check_arity(argc, 1, 2);
io = argv[0];
do {
obj = rb_funcallv(klass, rb_intern("new"), argc, argv);
if (rb_block_given_p()) {
rb_gzreader_each(0, 0, obj);
}
else {
if (!buf) {
buf = rb_str_new(0, 0);
}
tmpbuf = gzfile_read_all(get_gzfile(obj));
rb_str_cat(buf, RSTRING_PTR(tmpbuf), RSTRING_LEN(tmpbuf));
}
rb_gzreader_read(0, 0, obj);
pos = NUM2LONG(rb_funcall(io, rb_intern("pos"), 0));
unused = rb_gzreader_unused(obj);
rb_gzfile_finish(obj);
if (!NIL_P(unused)) {
pos -= NUM2LONG(rb_funcall(unused, rb_intern("length"), 0));
rb_funcall(io, rb_intern("pos="), 1, LONG2NUM(pos));
}
} while (pos < NUM2LONG(rb_funcall(io, rb_intern("size"), 0)));
if (rb_block_given_p()) {
return Qnil;
}
return buf;
}
Decompresses all gzip data in the io, handling multiple gzip streams until the end of the io. There should not be any non-gzip data after the gzip streams.
If a block is given, it is yielded strings of uncompressed data, and the method returns nil. If a block is not given, the method returns the concatenation of all uncompressed data in all gzip streams.