Method: Zstdlib::Deflate#initialize

Defined in:
ext/zstdlib_c/ruby/zlib-3.2/zstdlib.c,
ext/zstdlib_c/ruby/zlib-3.1/zstdlib.c,
ext/zstdlib_c/ruby/zlib-3.0/zstdlib.c,
ext/zstdlib_c/ruby/zlib-2.7/zstdlib.c,
ext/zstdlib_c/ruby/zlib-2.6/zstdlib.c,
ext/zstdlib_c/ruby/zlib-2.5/zstdlib.c,
ext/zstdlib_c/ruby/zlib-2.4/zstdlib.c,
ext/zstdlib_c/ruby/zlib-2.3/zstdlib.c,
ext/zstdlib_c/ruby/zlib-2.2/zstdlib.c

#initialize(*args) ⇒ Object

call-seq: Zstdlib::Deflate.new(level=DEFAULT_COMPRESSION, window_bits=MAX_WBITS, mem_level=DEF_MEM_LEVEL, strategy=DEFAULT_STRATEGY)

Creates a new deflate stream for compression. If a given argument is nil, the default value of that argument is used.

The +level+ sets the compression level for the deflate stream between 0 (no compression) and 9 (best compression). The following constants have been defined to make code more readable:

  • Zstdlib::DEFAULT_COMPRESSION
  • Zstdlib::NO_COMPRESSION
  • Zstdlib::BEST_SPEED
  • Zstdlib::BEST_COMPRESSION

See http://www.zlib.net/manual.html#Constants for further information.

The +window_bits+ sets the size of the history buffer and should be between 8 and 15. Larger values of this parameter result in better compression at the expense of memory usage.

The +mem_level+ specifies how much memory should be allocated for the internal compression state. 1 uses minimum memory but is slow and reduces compression ratio while 9 uses maximum memory for optimal speed. The default value is 8. Two constants are defined:

  • Zstdlib::DEF_MEM_LEVEL
  • Zstdlib::MAX_MEM_LEVEL

The +strategy+ sets the deflate compression strategy. The following strategies are available:

Zstdlib::DEFAULT_STRATEGY:: For normal data Zstdlib::FILTERED:: For data produced by a filter or predictor Zstdlib::FIXED:: Prevents dynamic Huffman codes Zstdlib::HUFFMAN_ONLY:: Prevents string matching Zstdlib::RLE:: Designed for better compression of PNG image data

See the constants for further description.

== Examples

=== Basic

open "compressed.file", "w+" do |io| io << Zstdlib::Deflate.new.deflate(File.read("big.file")) end

=== Custom compression

open "compressed.file", "w+" do |compressed_io| deflate = Zstdlib::Deflate.new(Zstdlib::BEST_COMPRESSION, Zstdlib::MAX_WBITS, Zstdlib::MAX_MEM_LEVEL, Zstdlib::HUFFMAN_ONLY)

begin
  open "big.file" do |big_io|
    until big_io.eof? do
      compressed_io << zd.deflate(big_io.read(16384))
    end
  end
ensure
  deflate.close
end

end

While this example will work, for best optimization review the flags for your specific time, memory usage and output space requirements.



1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
# File 'ext/zstdlib_c/ruby/zlib-3.2/zstdlib.c', line 1665

static VALUE
rb_deflate_initialize(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);
    TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, 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;
}