Class: Zlib::GzipFile

Inherits:
Object
  • Object
show all
Defined in:
ext/zlib/zlib.c,
ext/zlib/zlib.c

Overview

Zlib::GzipFile is an abstract class for handling a gzip formatted compressed file. The operations are defined in the subclasses, Zlib::GzipReader for reading, and Zlib::GzipWriter for writing.

GzipReader should be used by associating an IO, or IO-like, object.

Method Catalogue

  • ::wrap

  • ::open (Zlib::GzipReader::open and Zlib::GzipWriter::open)

  • #close

  • #closed?

  • #comment

  • comment= (Zlib::GzipWriter#comment=)

  • #crc

  • eof? (Zlib::GzipReader#eof?)

  • #finish

  • #level

  • lineno (Zlib::GzipReader#lineno)

  • lineno= (Zlib::GzipReader#lineno=)

  • #mtime

  • mtime= (Zlib::GzipWriter#mtime=)

  • #orig_name

  • orig_name (Zlib::GzipWriter#orig_name=)

  • #os_code

  • path (when the underlying IO supports #path)

  • #sync

  • #sync=

  • #to_io

(due to internal structure, documentation may appear under Zlib::GzipReader or Zlib::GzipWriter)

Direct Known Subclasses

GzipReader, GzipWriter

Defined Under Namespace

Classes: CRCError, Error, LengthError, NoFooter

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.wrap(*args) ⇒ Object

call-seq:

Zlib::GzipReader.wrap(io, ...) { |gz| ... }
Zlib::GzipWriter.wrap(io, ...) { |gz| ... }

Creates a GzipReader or GzipWriter associated with io, passing in any necessary extra options, and executes the block with the newly created object just like File.open.

The GzipFile object will be closed automatically after executing the block. If you want to keep the associated IO object open, you may call Zlib::GzipFile#finish method in the block.



3192
3193
3194
3195
3196
# File 'ext/zlib/zlib.c', line 3192

static VALUE
rb_gzfile_s_wrap(int argc, VALUE *argv, VALUE klass)
{
    return gzfile_wrap(argc, argv, klass, 0);
}

Instance Method Details

#closeObject

Closes the GzipFile object. This method calls close method of the associated IO object. Returns the associated IO object.



3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
# File 'ext/zlib/zlib.c', line 3416

static VALUE
rb_gzfile_close(VALUE obj)
{
    struct gzfile *gz;
    VALUE io;

    TypedData_Get_Struct(obj, struct gzfile, &gzfile_data_type, gz);
    if (!ZSTREAM_IS_READY(&gz->z)) {
        return Qnil;
    }
    io = gz->io;
    gzfile_close(gz, 1);
    return io;
}

#closed?Boolean

Same as IO#closed?

Returns:

  • (Boolean)


3455
3456
3457
3458
3459
3460
3461
# File 'ext/zlib/zlib.c', line 3455

static VALUE
rb_gzfile_closed_p(VALUE obj)
{
    struct gzfile *gz;
    TypedData_Get_Struct(obj, struct gzfile, &gzfile_data_type, gz);
    return NIL_P(gz->io) ? Qtrue : Qfalse;
}

#commentObject

Returns comments recorded in the gzip file header, or nil if the comments is not present.



3292
3293
3294
3295
3296
3297
3298
3299
3300
# File 'ext/zlib/zlib.c', line 3292

static VALUE
rb_gzfile_comment(VALUE obj)
{
    VALUE str = get_gzfile(obj)->comment;
    if (!NIL_P(str)) {
	str = rb_str_dup(str);
    }
    return str;
}

#crcObject

Returns CRC value of the uncompressed data.



3231
3232
3233
3234
3235
# File 'ext/zlib/zlib.c', line 3231

static VALUE
rb_gzfile_crc(VALUE obj)
{
    return rb_uint2inum(get_gzfile(obj)->crc);
}

#finishObject

Closes the GzipFile object. Unlike Zlib::GzipFile#close, this method never calls the close method of the associated IO object. Returns the associated IO object.



3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
# File 'ext/zlib/zlib.c', line 3438

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

    io = gz->io;
    gzfile_close(gz, 0);
    return io;
}

#levelObject

Returns compression level.



3253
3254
3255
3256
3257
# File 'ext/zlib/zlib.c', line 3253

static VALUE
rb_gzfile_level(VALUE obj)
{
    return INT2FIX(get_gzfile(obj)->level);
}

#mtimeObject

Returns last modification time recorded in the gzip file header.



3242
3243
3244
3245
3246
# File 'ext/zlib/zlib.c', line 3242

static VALUE
rb_gzfile_mtime(VALUE obj)
{
    return rb_time_new(get_gzfile(obj)->mtime, (time_t)0);
}

#orig_nameObject

Returns original filename recorded in the gzip file header, or nil if original filename is not present.



3276
3277
3278
3279
3280
3281
3282
3283
3284
# File 'ext/zlib/zlib.c', line 3276

static VALUE
rb_gzfile_orig_name(VALUE obj)
{
    VALUE str = get_gzfile(obj)->orig_name;
    if (!NIL_P(str)) {
	str = rb_str_dup(str);
    }
    return str;
}

#os_codeObject

Returns OS code number recorded in the gzip file header.



3264
3265
3266
3267
3268
# File 'ext/zlib/zlib.c', line 3264

static VALUE
rb_gzfile_os_code(VALUE obj)
{
    return INT2FIX(get_gzfile(obj)->os_code);
}

#syncObject

Same as IO#sync



3481
3482
3483
3484
3485
# File 'ext/zlib/zlib.c', line 3481

static VALUE
rb_gzfile_sync(VALUE obj)
{
    return (get_gzfile(obj)->z.flags & GZFILE_FLAG_SYNC) ? Qtrue : Qfalse;
}

#sync=(mode) ⇒ Object

call-seq: sync = flag

Same as IO. If flag is true, the associated IO object must respond to the flush method. While sync mode is true, the compression ratio decreases sharply.



3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
# File 'ext/zlib/zlib.c', line 3496

static VALUE
rb_gzfile_set_sync(VALUE obj, VALUE mode)
{
    struct gzfile *gz = get_gzfile(obj);

    if (RTEST(mode)) {
	gz->z.flags |= GZFILE_FLAG_SYNC;
    }
    else {
	gz->z.flags &= ~GZFILE_FLAG_SYNC;
    }
    return mode;
}

#to_ioObject

Same as IO.



3220
3221
3222
3223
3224
# File 'ext/zlib/zlib.c', line 3220

static VALUE
rb_gzfile_to_io(VALUE obj)
{
    return get_gzfile(obj)->io;
}