Class: Zlib::GzipFile

Inherits:
Object
  • Object
show all
Defined in:
zlib.c,
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

.wrapObject

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.



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

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.



3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
# File 'zlib.c', line 3255

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

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

#closed?Boolean

Same as IO#closed?

Returns:

  • (Boolean)


3290
3291
3292
3293
3294
3295
3296
# File 'zlib.c', line 3290

static VALUE
rb_gzfile_closed_p(VALUE obj)
{
    struct gzfile *gz;
    Data_Get_Struct(obj, struct gzfile, 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.



3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
# File 'zlib.c', line 3140

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

#crcObject

Returns CRC value of the uncompressed data.



3078
3079
3080
3081
3082
# File 'zlib.c', line 3078

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.



3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
# File 'zlib.c', line 3273

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.



3100
3101
3102
3103
3104
# File 'zlib.c', line 3100

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

#mtimeObject

Returns last modification time recorded in the gzip file header.



3089
3090
3091
3092
3093
# File 'zlib.c', line 3089

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.



3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
# File 'zlib.c', line 3123

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

#os_codeObject

Returns OS code number recorded in the gzip file header.



3111
3112
3113
3114
3115
# File 'zlib.c', line 3111

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

#syncObject

Same as IO#sync



3316
3317
3318
3319
3320
# File 'zlib.c', line 3316

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

#sync=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.



3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
# File 'zlib.c', line 3331

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.



3067
3068
3069
3070
3071
# File 'zlib.c', line 3067

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