Class: Zlib::GzipReader

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

Overview

Zlib::GzipReader is the class for reading a gzipped file. GzipReader should be used an IO, or -IO-lie, 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

# TODO: test these.  Are they equivalent?  Can GzipReader.new take a
# block?

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

#Zlib::GzipReader.new(io) ⇒ Object

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

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



2916
2917
2918
# File 'zlib.c', line 2916

static VALUE
rb_gzreader_initialize(obj, io)
VALUE obj, io;

Class Method Details

.Zlib::GzipReader.open(filename) {|gz| ... } ⇒ Object

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::GzipReader.wrap.

Yields:

  • (gz)


2897
2898
2899
# File 'zlib.c', line 2897

static VALUE
rb_gzreader_s_open(argc, argv, klass)
int argc;

Instance Method Details

#eachObject

See Zlib::GzipReader documentation for a description.



3207
3208
3209
# File 'zlib.c', line 3207

static VALUE
rb_gzreader_each(argc, argv, obj)
int argc;

#each_byteObject

See Zlib::GzipReader documentation for a description.



3023
3024
3025
# File 'zlib.c', line 3023

static VALUE
rb_gzreader_each_byte(obj)
VALUE obj;

#each_lineObject

See Zlib::GzipReader documentation for a description.



3207
3208
3209
# File 'zlib.c', line 3207

static VALUE
rb_gzreader_each(argc, argv, obj)
int argc;

#eofObject

???



2593
2594
2595
# File 'zlib.c', line 2593

static VALUE
rb_gzfile_eof_p(obj)
VALUE obj;

#eof?Boolean

???

Returns:

  • (Boolean)


2593
2594
2595
# File 'zlib.c', line 2593

static VALUE
rb_gzfile_eof_p(obj)
VALUE obj;

#getcObject

See Zlib::GzipReader documentation for a description.



2991
2992
2993
# File 'zlib.c', line 2991

static VALUE
rb_gzreader_getc(obj)
VALUE obj;

#getsObject

See Zlib::GzipReader documentation for a description.



3173
3174
3175
# File 'zlib.c', line 3173

static VALUE
rb_gzreader_gets(argc, argv, obj)
int argc;

#linenoObject

???



2456
2457
2458
# File 'zlib.c', line 2456

static VALUE
rb_gzfile_lineno(obj)
VALUE obj;

#lineno=Object

???



2466
2467
2468
# File 'zlib.c', line 2466

static VALUE
rb_gzfile_set_lineno(obj, lineno)
VALUE obj, lineno;

#posObject

???



2646
2647
2648
# File 'zlib.c', line 2646

static VALUE
rb_gzfile_total_out(obj)
VALUE obj;

#readObject

See Zlib::GzipReader documentation for a description.



2966
2967
2968
# File 'zlib.c', line 2966

static VALUE
rb_gzreader_read(argc, argv, obj)
int argc;

#readcharObject

See Zlib::GzipReader documentation for a description.



3008
3009
3010
# File 'zlib.c', line 3008

static VALUE
rb_gzreader_readchar(obj)
VALUE obj;

#readlineObject

See Zlib::GzipReader documentation for a description.



3190
3191
3192
# File 'zlib.c', line 3190

static VALUE
rb_gzreader_readline(argc, argv, obj)
int argc;

#readlinesObject

See Zlib::GzipReader documentation for a description.



3223
3224
3225
# File 'zlib.c', line 3223

static VALUE
rb_gzreader_readlines(argc, argv, obj)
int argc;

#rewindObject

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.



2941
2942
2943
# File 'zlib.c', line 2941

static VALUE
rb_gzreader_rewind(obj)
VALUE obj;

#tellObject

???



2646
2647
2648
# File 'zlib.c', line 2646

static VALUE
rb_gzfile_total_out(obj)
VALUE obj;

#ungetcObject

See Zlib::GzipReader documentation for a description.



3037
3038
3039
# File 'zlib.c', line 3037

static VALUE
rb_gzreader_ungetc(obj, ch)
VALUE obj, ch;

#unusedObject

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



2954
2955
2956
# File 'zlib.c', line 2954

static VALUE
rb_gzreader_unused(obj)
VALUE obj;