Class: Zlib::GzipFile

Inherits:
Object
  • Object
show all
Defined in:
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::GzipFile.wrap(io) { |gz| ... }

Creates a GzipFile object associated with io, and executes the block with the newly created GzipFile 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 opening, you may call Zlib::GzipFile#finish method in the block.



2780
2781
2782
2783
2784
# File 'zlib.c', line 2780

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.



2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
# File 'zlib.c', line 2998

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)


3033
3034
3035
3036
3037
3038
3039
# File 'zlib.c', line 3033

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.



2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
# File 'zlib.c', line 2883

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.



2821
2822
2823
2824
2825
# File 'zlib.c', line 2821

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.



3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
# File 'zlib.c', line 3016

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.



2843
2844
2845
2846
2847
# File 'zlib.c', line 2843

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

#mtimeObject

Returns last modification time recorded in the gzip file header.



2832
2833
2834
2835
2836
# File 'zlib.c', line 2832

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.



2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
# File 'zlib.c', line 2866

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.



2854
2855
2856
2857
2858
# File 'zlib.c', line 2854

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

#syncObject

Same as IO#sync



3059
3060
3061
3062
3063
# File 'zlib.c', line 3059

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.



3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
# File 'zlib.c', line 3074

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.



2810
2811
2812
2813
2814
# File 'zlib.c', line 2810

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