Class: Zlib::GzipReader

Inherits:
GzipFile show all
Includes:
Enumerable
Defined in:
ext/zlib/zlib.c,
ext/zlib/zlib.c

Overview

Zlib::GzipReader is the class for reading a gzipped file. GzipReader should be used as an IO, or -IO-like, object.

Zlib::GzipReader.open('hoge.gz') {|gz|
print gz.read
}

File.open('hoge.gz') do |f|
gz = Zlib::GzipReader.new(f)
print gz.read
gz.close
end

Method Catalogue

The following methods in Zlib::GzipReader are just like their counterparts in IO, but they raise Zlib::Error or Zlib::GzipFile::Error exception if an error was found in the gzip file.

  • #each
  • #each_line
  • #each_byte
  • #gets
  • #getc
  • #lineno
  • #lineno=
  • #read
  • #readchar
  • #readline
  • #readlines
  • #ungetc

Be careful of the footer of the gzip file. A gzip file has the checksum of pre-compressed data in its footer. GzipReader checks all uncompressed data against that checksum at the following cases, and if it fails, raises Zlib::GzipFile::NoFooter, Zlib::GzipFile::CRCError, or Zlib::GzipFile::LengthError exception.

  • When an reading request is received beyond the end of file (the end of compressed data). That is, when Zlib::GzipReader#read, Zlib::GzipReader#gets, or some other methods for reading returns nil.
  • When Zlib::GzipFile#close method is called after the object reaches the end of file.
  • When Zlib::GzipReader#unused method is called after the object reaches the end of file.

The rest of the methods are adequately described in their own documentation.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GzipFile

#close, #closed?, #comment, #crc, #finish, #level, #mtime, #orig_name, #os_code, #sync, #sync=, #to_io, wrap

Constructor Details

#initialize(*args) ⇒ Object

call-seq:

Zlib::GzipReader.new(io, options = {})

Creates a GzipReader object associated with io. The GzipReader object reads gzipped data from io, and parses/decompresses it. The io must have a read method that behaves same as the IO#read.

The options hash may be used to set the encoding of the data. :external_encoding, :internal_encoding and :encoding may be set as in IO::new.

If the gzip file header is incorrect, raises an Zlib::GzipFile::Error exception.



4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
# File 'ext/zlib/zlib.c', line 4171

static VALUE
rb_gzreader_initialize(int argc, VALUE *argv, VALUE obj)
{
    VALUE io, opt = Qnil;
    struct gzfile *gz;
    int err;

    TypedData_Get_Struct(obj, struct gzfile, &gzfile_data_type, gz);
    rb_scan_args(argc, argv, "1:", &io, &opt);

    /* this is undocumented feature of zlib */
    err = inflateInit2(&gz->z.stream, -MAX_WBITS);
    if (err != Z_OK) {
	raise_zlib_error(err, gz->z.stream.msg);
    }
    gz->io = io;
    ZSTREAM_READY(&gz->z);
    gzfile_read_header(gz, Qnil);
    rb_gzfile_ecopts(gz, opt);

    if (rb_respond_to(io, id_path)) {
	/* File#path may raise IOError in case when a path is unavailable */
	rb_rescue2(gzfile_initialize_path_partial, obj, NULL, Qnil, rb_eIOError, (VALUE)0);
    }

    return obj;
}

Class Method Details

.open(*args) ⇒ Object

call-seq: Zlib::GzipReader.open(filename) {|gz| ... }

Opens a file specified by filename as a gzipped file, and returns a GzipReader object associated with that file. Further details of this method are in Zlib::GzipReader.new and ZLib::GzipFile.wrap.



4093
4094
4095
4096
4097
# File 'ext/zlib/zlib.c', line 4093

static VALUE
rb_gzreader_s_open(int argc, VALUE *argv, VALUE klass)
{
    return gzfile_s_open(argc, argv, klass, "rb");
}

.zcat(*args) ⇒ Object

call-seq:

Zlib::GzipReader.zcat(io, options = {}, &block) => nil
Zlib::GzipReader.zcat(io, options = {}) => string

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.



4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
# File 'ext/zlib/zlib.c', line 4115

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_kw(klass, rb_intern("new"), argc, argv, RB_PASS_CALLED_KEYWORDS);
        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), Qnil);
            rb_str_cat(buf, RSTRING_PTR(tmpbuf), RSTRING_LEN(tmpbuf));
            RB_GC_GUARD(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;
}

Instance Method Details

#each(*args) ⇒ Object

See Zlib::GzipReader documentation for a description.



4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
# File 'ext/zlib/zlib.c', line 4677

static VALUE
rb_gzreader_each(int argc, VALUE *argv, VALUE obj)
{
    VALUE str;

    RETURN_ENUMERATOR(obj, argc, argv);

    gzreader_check_limit(argc, argv, "each_line");
    while (!NIL_P(str = gzreader_gets(argc, argv, obj))) {
	rb_yield(str);
    }
    return obj;
}

#each_byte ⇒ Object

See Zlib::GzipReader documentation for a description.



4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
# File 'ext/zlib/zlib.c', line 4369

static VALUE
rb_gzreader_each_byte(VALUE obj)
{
    VALUE c;

    RETURN_ENUMERATOR(obj, 0, 0);

    while (!NIL_P(c = rb_gzreader_getbyte(obj))) {
	rb_yield(c);
    }
    return Qnil;
}

#each_char ⇒ Object

See Zlib::GzipReader documentation for a description.



4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
# File 'ext/zlib/zlib.c', line 4351

static VALUE
rb_gzreader_each_char(VALUE obj)
{
    VALUE c;

    RETURN_ENUMERATOR(obj, 0, 0);

    while (!NIL_P(c = rb_gzreader_getc(obj))) {
	rb_yield(c);
    }
    return Qnil;
}

#each_line(*args) ⇒ Object

See Zlib::GzipReader documentation for a description.



4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
# File 'ext/zlib/zlib.c', line 4677

static VALUE
rb_gzreader_each(int argc, VALUE *argv, VALUE obj)
{
    VALUE str;

    RETURN_ENUMERATOR(obj, argc, argv);

    gzreader_check_limit(argc, argv, "each_line");
    while (!NIL_P(str = gzreader_gets(argc, argv, obj))) {
	rb_yield(str);
    }
    return obj;
}

#eof ⇒ Object

Returns true or false whether the stream has reached the end.



3720
3721
3722
3723
3724
3725
3726
3727
3728
# File 'ext/zlib/zlib.c', line 3720

static VALUE
rb_gzfile_eof_p(VALUE obj)
{
    struct gzfile *gz = get_gzfile(obj);
    while (!ZSTREAM_IS_FINISHED(&gz->z) && ZSTREAM_BUF_FILLED(&gz->z) == 0) {
	gzfile_read_more(gz, Qnil);
    }
    return GZFILE_IS_FINISHED(gz) ? Qtrue : Qfalse;
}

#eof? ⇒ Boolean

Returns true or false whether the stream has reached the end.

Returns:

  • (Boolean)


3720
3721
3722
3723
3724
3725
3726
3727
3728
# File 'ext/zlib/zlib.c', line 3720

static VALUE
rb_gzfile_eof_p(VALUE obj)
{
    struct gzfile *gz = get_gzfile(obj);
    while (!ZSTREAM_IS_FINISHED(&gz->z) && ZSTREAM_BUF_FILLED(&gz->z) == 0) {
	gzfile_read_more(gz, Qnil);
    }
    return GZFILE_IS_FINISHED(gz) ? Qtrue : Qfalse;
}

#external_encoding ⇒ Object

See Zlib::GzipReader documentation for a description.



4713
4714
4715
4716
4717
4718
4719
4720
# File 'ext/zlib/zlib.c', line 4713

static VALUE
rb_gzreader_external_encoding(VALUE self)
{
    struct gzfile *gz = get_gzfile(self);
    /* with a conversion set up, enc is the internal encoding and enc2 the
     * external one (same convention as rb_io_extract_encoding_option) */
    return rb_enc_from_encoding(gz->enc2 ? gz->enc2 : gz->enc);
}

#getbyte ⇒ Object

See Zlib::GzipReader documentation for a description.



4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
# File 'ext/zlib/zlib.c', line 4317

static VALUE
rb_gzreader_getbyte(VALUE obj)
{
    struct gzfile *gz = get_gzfile(obj);
    VALUE dst;

    dst = gzfile_read(gz, 1, Qnil);
    if (!NIL_P(dst)) {
	dst = INT2FIX((unsigned int)(RSTRING_PTR(dst)[0]) & 0xff);
    }
    return dst;
}

#getc ⇒ Object

See Zlib::GzipReader documentation for a description.



4288
4289
4290
4291
4292
4293
4294
# File 'ext/zlib/zlib.c', line 4288

static VALUE
rb_gzreader_getc(VALUE obj)
{
    struct gzfile *gz = get_gzfile(obj);

    return gzfile_getc(gz);
}

#gets(*args) ⇒ Object

See Zlib::GzipReader documentation for a description. However, note that this method can return nil even if #eof? returns false, unlike the behavior of File#gets.



4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
# File 'ext/zlib/zlib.c', line 4626

static VALUE
rb_gzreader_gets(int argc, VALUE *argv, VALUE obj)
{
    VALUE dst;
    dst = gzreader_gets(argc, argv, obj);
    if (!NIL_P(dst)) {
	rb_lastline_set(dst);
    }
    return dst;
}

#lineno ⇒ Object

The line number of the last row read from this file.



3559
3560
3561
3562
3563
# File 'ext/zlib/zlib.c', line 3559

static VALUE
rb_gzfile_lineno(VALUE obj)
{
    return INT2NUM(get_gzfile(obj)->lineno);
}

#lineno=(lineno) ⇒ Object

Specify line number of the last row read from this file.



3570
3571
3572
3573
3574
3575
3576
# File 'ext/zlib/zlib.c', line 3570

static VALUE
rb_gzfile_set_lineno(VALUE obj, VALUE lineno)
{
    struct gzfile *gz = get_gzfile(obj);
    gz->lineno = NUM2INT(lineno);
    return lineno;
}

#pos ⇒ Object

Total number of output bytes output so far.



3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
# File 'ext/zlib/zlib.c', line 3781

static VALUE
rb_gzfile_total_out(VALUE obj)
{
    struct gzfile *gz = get_gzfile(obj);
    uLong total_out = gz->z.stream.total_out;
    long buf_filled = ZSTREAM_BUF_FILLED(&gz->z);

    if (total_out >= (uLong)buf_filled) {
        return rb_uint2inum(total_out - buf_filled);
    } else {
        return LONG2FIX(-(buf_filled - (long)total_out));
    }
}

#read(*args) ⇒ Object

See Zlib::GzipReader documentation for a description.



4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
# File 'ext/zlib/zlib.c', line 4232

static VALUE
rb_gzreader_read(int argc, VALUE *argv, VALUE obj)
{
    struct gzfile *gz = get_gzfile(obj);
    VALUE vlen, outbuf;
    rb_len_t len;

    rb_scan_args(argc, argv, "02", &vlen, &outbuf);
    if (!NIL_P(outbuf))
        Check_Type(outbuf, T_STRING);
    if (NIL_P(vlen)) {
	return gzfile_read_all(gz, outbuf);
    }

    len = NUM2INT(vlen);
    if (len < 0) {
	rb_raise(rb_eArgError, "negative length %"PRIdLEN" given", len);
    }
    return gzfile_read(gz, len, outbuf);
}

#readbyte ⇒ Object

See Zlib::GzipReader documentation for a description.



4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
# File 'ext/zlib/zlib.c', line 4335

static VALUE
rb_gzreader_readbyte(VALUE obj)
{
    VALUE dst;
    dst = rb_gzreader_getbyte(obj);
    if (NIL_P(dst)) {
	rb_raise(rb_eEOFError, "end of file reached");
    }
    return dst;
}

#readchar ⇒ Object

See Zlib::GzipReader documentation for a description.



4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
# File 'ext/zlib/zlib.c', line 4301

static VALUE
rb_gzreader_readchar(VALUE obj)
{
    VALUE dst;
    dst = rb_gzreader_getc(obj);
    if (NIL_P(dst)) {
	rb_raise(rb_eEOFError, "end of file reached");
    }
    return dst;
}

#readline(*args) ⇒ Object

See Zlib::GzipReader documentation for a description.



4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
# File 'ext/zlib/zlib.c', line 4642

static VALUE
rb_gzreader_readline(int argc, VALUE *argv, VALUE obj)
{
    VALUE dst;
    dst = rb_gzreader_gets(argc, argv, obj);
    if (NIL_P(dst)) {
	rb_raise(rb_eEOFError, "end of file reached");
    }
    return dst;
}

#readlines(*args) ⇒ Object

See Zlib::GzipReader documentation for a description.



4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
# File 'ext/zlib/zlib.c', line 4696

static VALUE
rb_gzreader_readlines(int argc, VALUE *argv, VALUE obj)
{
    VALUE str, dst;
    gzreader_check_limit(argc, argv, "readlines");
    dst = rb_ary_new();
    while (!NIL_P(str = gzreader_gets(argc, argv, obj))) {
	rb_ary_push(dst, str);
    }
    return dst;
}

#readpartial(*args) ⇒ Object

call-seq: gzipreader.readpartial(maxlen [, outbuf]) => string, outbuf

Reads at most maxlen bytes from the gzipped stream but it blocks only if gzipreader has no data immediately available. If the optional outbuf argument is present, it must reference a String, which will receive the data. It raises EOFError on end of file.



4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
# File 'ext/zlib/zlib.c', line 4265

static VALUE
rb_gzreader_readpartial(int argc, VALUE *argv, VALUE obj)
{
    struct gzfile *gz = get_gzfile(obj);
    VALUE vlen, outbuf;
    rb_len_t len;

    rb_scan_args(argc, argv, "11", &vlen, &outbuf);

    len = NUM2INT(vlen);
    if (len < 0) {
	rb_raise(rb_eArgError, "negative length %"PRIdLEN" given", len);
    }
    if (!NIL_P(outbuf))
        Check_Type(outbuf, T_STRING);
    return gzfile_readpartial(gz, len, outbuf);
}

#rewind ⇒ Object

Resets the position of the file pointer to the point created the GzipReader object. The associated IO object needs to respond to the seek method.



4205
4206
4207
4208
4209
4210
4211
# File 'ext/zlib/zlib.c', line 4205

static VALUE
rb_gzreader_rewind(VALUE obj)
{
    struct gzfile *gz = get_gzfile(obj);
    gzfile_reader_rewind(gz);
    return INT2FIX(0);
}

#tell ⇒ Object

Total number of output bytes output so far.



3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
# File 'ext/zlib/zlib.c', line 3781

static VALUE
rb_gzfile_total_out(VALUE obj)
{
    struct gzfile *gz = get_gzfile(obj);
    uLong total_out = gz->z.stream.total_out;
    long buf_filled = ZSTREAM_BUF_FILLED(&gz->z);

    if (total_out >= (uLong)buf_filled) {
        return rb_uint2inum(total_out - buf_filled);
    } else {
        return LONG2FIX(-(buf_filled - (long)total_out));
    }
}

#ungetbyte(ch) ⇒ Object

See Zlib::GzipReader documentation for a description.



4409
4410
4411
4412
4413
4414
4415
# File 'ext/zlib/zlib.c', line 4409

static VALUE
rb_gzreader_ungetbyte(VALUE obj, VALUE ch)
{
    struct gzfile *gz = get_gzfile(obj);
    gzfile_ungetbyte(gz, NUM2CHR(ch));
    return Qnil;
}

#ungetc(s) ⇒ Object

See Zlib::GzipReader documentation for a description.



4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
# File 'ext/zlib/zlib.c', line 4387

static VALUE
rb_gzreader_ungetc(VALUE obj, VALUE s)
{
    struct gzfile *gz;

    if (FIXNUM_P(s))
	return rb_gzreader_ungetbyte(obj, s);
    gz = get_gzfile(obj);
    StringValue(s);
    if (gz->enc2 && gz->enc2 != rb_ascii8bit_encoding()) {
	s = rb_str_conv_enc(s, rb_enc_get(s), gz->enc2);
    }
    gzfile_ungets(gz, (const Bytef*)RSTRING_PTR(s), RSTRING_LEN(s));
    RB_GC_GUARD(s);
    return Qnil;
}

#unused ⇒ Object

Returns the rest of the data which had read for parsing gzip format, or nil if the whole gzip file is not parsed yet.



4219
4220
4221
4222
4223
4224
4225
# File 'ext/zlib/zlib.c', line 4219

static VALUE
rb_gzreader_unused(VALUE obj)
{
    struct gzfile *gz;
    TypedData_Get_Struct(obj, struct gzfile, &gzfile_data_type, gz);
    return gzfile_reader_get_unused(gz);
}