Class | Zlib::Deflate
In: | zlib/zlib.c
Parent: | Zlib::ZStream

Zlib::Deflate is the class for compressing data. See Zlib::Stream for more information.

### Methods

<< deflate deflate flush initialize_copy new params set_dictionary

### Public Class methods

Zlib::Deflate.deflate(string[, level])

Compresses the given `string`. Valid values of level are `Zlib::NO_COMPRESSION`, `Zlib::BEST_SPEED`, `Zlib::BEST_COMPRESSION`, `Zlib::DEFAULT_COMPRESSION`, and an integer from 0 to 9.

This method is almost equivalent to the following code:

      def deflate(string, level)
        z = Zlib::Deflate.new(level)
        dst = z.deflate(string, Zlib::FINISH)
        z.close
        dst
      end

TODO: what’s default value of `level`?

[Source]

    /*
     * call-seq: Zlib::Deflate.deflate(string[, level])
     *
     * Compresses the given +string+. Valid values of level are
     * <tt>Zlib::NO_COMPRESSION</tt>, <tt>Zlib::BEST_SPEED</tt>,
     * <tt>Zlib::BEST_COMPRESSION</tt>, <tt>Zlib::DEFAULT_COMPRESSION</tt>, and an
     * integer from 0 to 9.
     *
     * This method is almost equivalent to the following code:
     *
     *   def deflate(string, level)
     *     z = Zlib::Deflate.new(level)
     *     dst = z.deflate(string, Zlib::FINISH)
     *     z.close
     *     dst
     *   end
     *
     * TODO: what's default value of +level+?
     *
     */
    static VALUE
    rb_deflate_s_deflate(argc, argv, klass)
        int argc;
        VALUE *argv;
        VALUE klass;
    {
        struct zstream z;
        VALUE src, level, dst, args[2];
        int err, lev;

        rb_scan_args(argc, argv, "11", &src, &level);

        lev = ARG_LEVEL(level);
        StringValue(src);
        zstream_init_deflate(&z);
        err = deflateInit(&z.stream, lev);
        if (err != Z_OK) {
    	raise_zlib_error(err, z.stream.msg);
        }
        ZSTREAM_READY(&z);

        args[0] = (VALUE)&z;
        args[1] = src;
        dst = rb_ensure(deflate_run, (VALUE)args, zstream_end, (VALUE)&z);

        OBJ_INFECT(dst, src);
        return dst;
    }

Zlib::Deflate.new(level=nil, windowBits=nil, memlevel=nil, strategy=nil)

Creates a new deflate stream for compression. See zlib.h for details of each argument. If an argument is nil, the default value of that argument is used.

TODO: document better!

[Source]

    /*
     * call-seq: Zlib::Deflate.new(level=nil, windowBits=nil, memlevel=nil, strategy=nil)
     *
     * Creates a new deflate stream for compression. See zlib.h for details of
     * each argument. If an argument is nil, the default value of that argument is
     * used.
     *
     * TODO: document better!
     */
    static VALUE
    rb_deflate_initialize(argc, argv, obj)
        int argc;
        VALUE *argv;
        VALUE obj;
    {
        struct zstream *z;
        VALUE level, wbits, memlevel, strategy;
        int err;

        rb_scan_args(argc, argv, "04", &level, &wbits, &memlevel, &strategy);
        Data_Get_Struct(obj, struct zstream, z);

        err = deflateInit2(&z->stream, ARG_LEVEL(level), Z_DEFLATED,
    		       ARG_WBITS(wbits), ARG_MEMLEVEL(memlevel),
    		       ARG_STRATEGY(strategy));
        if (err != Z_OK) {
    	raise_zlib_error(err, z->stream.msg);
        }
        ZSTREAM_READY(z);

        return obj;
    }

### Public Instance methods

<<(p1)

Same as IO.

[Source]

    /*
     * call-seq: << string
     *
     * Inputs +string+ into the deflate stream just like Zlib::Deflate#deflate, but
     * returns the Zlib::Deflate object itself.  The output from the stream is
     * preserved in output buffer.
     */
    static VALUE
    rb_deflate_addstr(obj, src)
        VALUE obj, src;
    {
        OBJ_INFECT(obj, src);
        do_deflate(get_zstream(obj), src, Z_NO_FLUSH);
        return obj;
    }

deflate(string[, flush])

Inputs `string` into the deflate stream and returns the output from the stream. On calling this method, both the input and the output buffers of the stream are flushed. If `string` is nil, this method finishes the stream, just like Zlib::ZStream#finish.

The value of `flush` should be either `Zlib::NO_FLUSH`, `Zlib::SYNC_FLUSH`, `Zlib::FULL_FLUSH`, or `Zlib::FINISH`. See zlib.h for details.

TODO: document better!

[Source]

    /*
     * call-seq: deflate(string[, flush])
     *
     * Inputs +string+ into the deflate stream and returns the output from the
     * stream.  On calling this method, both the input and the output buffers of
     * the stream are flushed. If +string+ is nil, this method finishes the
     * stream, just like Zlib::ZStream#finish.
     *
     * The value of +flush+ should be either <tt>Zlib::NO_FLUSH</tt>,
     * <tt>Zlib::SYNC_FLUSH</tt>, <tt>Zlib::FULL_FLUSH</tt>, or
     * <tt>Zlib::FINISH</tt>. See zlib.h for details.
     *
     * TODO: document better!
     */
    static VALUE
    rb_deflate_deflate(argc, argv, obj)
        int argc;
        VALUE *argv;
        VALUE obj;
    {
        struct zstream *z = get_zstream(obj);
        VALUE src, flush, dst;

        rb_scan_args(argc, argv, "11", &src, &flush);
        OBJ_INFECT(obj, src);
        do_deflate(z, src, ARG_FLUSH(flush));
        dst = zstream_detach_buffer(z);

        OBJ_INFECT(dst, obj);
        return dst;
    }

flush(flush)

This method is equivalent to `deflate(’’, flush)`. If flush is omitted, `Zlib::SYNC_FLUSH` is used as flush. This method is just provided to improve the readability of your Ruby program.

TODO: document better!

[Source]

    /*
     * call-seq: flush(flush)
     *
     * This method is equivalent to <tt>deflate('', flush)</tt>.  If flush is omitted,
     * <tt>Zlib::SYNC_FLUSH</tt> is used as flush.  This method is just provided
     * to improve the readability of your Ruby program.
     *
     * TODO: document better!
     */
    static VALUE
    rb_deflate_flush(argc, argv, obj)
        int argc;
        VALUE *argv;
        VALUE obj;
    {
        struct zstream *z = get_zstream(obj);
        VALUE v_flush, dst;
        int flush;

        rb_scan_args(argc, argv, "01", &v_flush);
        flush = FIXNUMARG(v_flush, Z_SYNC_FLUSH);
        if (flush != Z_NO_FLUSH) {  /* prevent Z_BUF_ERROR */
    	zstream_run(z, "", 0, flush);
        }
        dst = zstream_detach_buffer(z);

        OBJ_INFECT(dst, obj);
        return dst;
    }

initialize_copy()

Duplicates the deflate stream.

[Source]

    /*
     * Duplicates the deflate stream.
     */
    static VALUE
    rb_deflate_init_copy(self, orig)
        VALUE self, orig;
    {
        struct zstream *z1 = get_zstream(self);
        struct zstream *z2 = get_zstream(orig);
        int err;

        err = deflateCopy(&z1->stream, &z2->stream);
        if (err != Z_OK) {
    	raise_zlib_error(err, 0);
        }
        z1->flags = z2->flags;

        return self;
    }

params(level, strategy)

Changes the parameters of the deflate stream. See zlib.h for details. The output from the stream by changing the params is preserved in output buffer.

TODO: document better!

[Source]

    /*
     * call-seq: params(level, strategy)
     *
     * Changes the parameters of the deflate stream. See zlib.h for details. The
     * output from the stream by changing the params is preserved in output
     * buffer.
     *
     * TODO: document better!
     */
    static VALUE
    rb_deflate_params(obj, v_level, v_strategy)
        VALUE obj, v_level, v_strategy;
    {
        struct zstream *z = get_zstream(obj);
        int level, strategy;
        int err;

        level = ARG_LEVEL(v_level);
        strategy = ARG_STRATEGY(v_strategy);

        err = deflateParams(&z->stream, level, strategy);
        while (err == Z_BUF_ERROR) {
    	if (RTEST(ruby_debug)) {
    	    rb_warning("deflateParams() returned Z_BUF_ERROR");
    	}
    	zstream_expand_buffer(z);
    	err = deflateParams(&z->stream, level, strategy);
        }
        if (err != Z_OK) {
    	raise_zlib_error(err, z->stream.msg);
        }

        return Qnil;
    }

set_dictionary(string)

Sets the preset dictionary and returns `string`. This method is available just only after Zlib::Deflate.new or Zlib::ZStream#reset method was called. See zlib.h for details.

TODO: document better!

[Source]

    /*
     * call-seq: set_dictionary(string)
     *
     * Sets the preset dictionary and returns +string+. This method is available
     * just only after Zlib::Deflate.new or Zlib::ZStream#reset method was called.
     * See zlib.h for details.
     *
     * TODO: document better!
     */
    static VALUE
    rb_deflate_set_dictionary(obj, dic)
        VALUE obj, dic;
    {
        struct zstream *z = get_zstream(obj);
        VALUE src = dic;
        int err;

        OBJ_INFECT(obj, dic);
        StringValue(src);
        err = deflateSetDictionary(&z->stream,
    			       RSTRING(src)->ptr, RSTRING(src)->len);
        if (err != Z_OK) {
    	raise_zlib_error(err, z->stream.msg);
        }

        return dic;
    }

[Validate]