Module: Zlib

Defined in:
zlib.c

Overview

The Zlib module contains several classes for compressing and decompressing streams, and for working with "gzip" files.

Classes

Following are the classes that are most likely to be of interest to the user: Zlib::Inflate Zlib::Deflate Zlib::GzipReader Zlib::GzipWriter

There are two important base classes for the classes above: Zlib::ZStream and Zlib::GzipFile. Everything else is an error class.

Constants

Here's a list.

Zlib::VERSION
    The Ruby/zlib version string.

Zlib::ZLIB_VERSION
    The string which represents the version of zlib.h.

Zlib::BINARY
Zlib::ASCII
Zlib::UNKNOWN
    The integers representing data types which Zlib::ZStream#data_type
    method returns.

Zlib::NO_COMPRESSION
Zlib::BEST_SPEED
Zlib::BEST_COMPRESSION
Zlib::DEFAULT_COMPRESSION
    The integers representing compression levels which are an argument
    for Zlib::Deflate.new, Zlib::Deflate#deflate, and so on.

Zlib::FILTERED
Zlib::HUFFMAN_ONLY
Zlib::DEFAULT_STRATEGY
    The integers representing compression methods which are an argument
    for Zlib::Deflate.new and Zlib::Deflate#params.

Zlib::DEF_MEM_LEVEL
Zlib::MAX_MEM_LEVEL
    The integers representing memory levels which are an argument for
    Zlib::Deflate.new, Zlib::Deflate#params, and so on.

Zlib::MAX_WBITS
    The default value of windowBits which is an argument for
    Zlib::Deflate.new and Zlib::Inflate.new.

Zlib::NO_FLUSH
Zlib::SYNC_FLUSH
Zlib::FULL_FLUSH
Zlib::FINISH
    The integers to control the output of the deflate stream, which are
    an argument for Zlib::Deflate#deflate and so on.

Zlib::OS_CODE
Zlib::OS_MSDOS
Zlib::OS_AMIGA
Zlib::OS_VMS
Zlib::OS_UNIX
Zlib::OS_VMCMS
Zlib::OS_ATARI
Zlib::OS_OS2
Zlib::OS_MACOS
Zlib::OS_ZSYSTEM
Zlib::OS_CPM
Zlib::OS_TOPS20
Zlib::OS_WIN32
Zlib::OS_QDOS
Zlib::OS_RISCOS
Zlib::OS_UNKNOWN
    The return values of Zlib::GzipFile#os_code method.

Defined Under Namespace

Classes: BufError, DataError, Deflate, Error, GzipFile, GzipReader, GzipWriter, Inflate, MemError, NeedDict, StreamEnd, StreamError, VersionError, ZStream

Constant Summary collapse

VERSION =

The Ruby/zlib version string.

rb_str_new2(RUBY_ZLIB_VERSION)
ZLIB_VERSION =

The string which represents the version of zlib.h

rb_str_new2(ZLIB_VERSION)
BINARY =

Integer representing date types which ZStream#data_type method returns

INT2FIX(Z_BINARY)
ASCII =

Integer representing date types which ZStream#data_type method returns

INT2FIX(Z_ASCII)
UNKNOWN =

Integer representing date types which ZStream#data_type method returns

INT2FIX(Z_UNKNOWN)
NO_COMPRESSION =

compression level 0

Which is an argument for Deflate.new, Deflate#deflate, and so on.

INT2FIX(Z_NO_COMPRESSION)
BEST_SPEED =

compression level 1

Which is an argument for Deflate.new, Deflate#deflate, and so on.

INT2FIX(Z_BEST_SPEED)
BEST_COMPRESSION =

compression level 9

Which is an argument for Deflate.new, Deflate#deflate, and so on.

INT2FIX(Z_BEST_COMPRESSION)
DEFAULT_COMPRESSION =

compression level -1

Which is an argument for Deflate.new, Deflate#deflate, and so on.

INT2FIX(Z_DEFAULT_COMPRESSION)
FILTERED =

compression method 1

Which is an argument for Deflate.new and Deflate#params.

INT2FIX(Z_FILTERED)
HUFFMAN_ONLY =

compression method 2

Which is an argument for Deflate.new and Deflate#params.

INT2FIX(Z_HUFFMAN_ONLY)
DEFAULT_STRATEGY =

compression method 0

Which is an argument for Deflate.new and Deflate#params.

INT2FIX(Z_DEFAULT_STRATEGY)
MAX_WBITS =

The default value of windowBits which is an argument for Deflate.new and Inflate.new.

INT2FIX(MAX_WBITS)
DEF_MEM_LEVEL =

Default value is 8

The integer representing memory levels. Which are an argument for Deflate.new, Deflate#params, and so on.

INT2FIX(DEF_MEM_LEVEL)
MAX_MEM_LEVEL =

Maximum level is 9

The integers representing memory levels which are an argument for Deflate.new, Deflate#params, and so on.

INT2FIX(MAX_MEM_LEVEL)
NO_FLUSH =

Output control - 0

The integers to control the output of the deflate stream, which are an argument for Deflate#deflate and so on.

INT2FIX(Z_NO_FLUSH)
SYNC_FLUSH =

Output control - 2

The integers to control the output of the deflate stream, which are an argument for Deflate#deflate and so on.

INT2FIX(Z_SYNC_FLUSH)
FULL_FLUSH =

Output control - 3

The integers to control the output of the deflate stream, which are an argument for Deflate#deflate and so on.

INT2FIX(Z_FULL_FLUSH)
FINISH =

Oputput control - 4

The integers to control the output of the deflate stream, which are an argument for Deflate#deflate and so on.

INT2FIX(Z_FINISH)
OS_CODE =

From GzipFile#os_code - code of current host

INT2FIX(OS_CODE)
OS_MSDOS =

From GzipFile#os_code - 0x00

INT2FIX(OS_MSDOS)
OS_AMIGA =

From GzipFile#os_code - 0x01

INT2FIX(OS_AMIGA)
OS_VMS =

From GzipFile#os_code - 0x02

INT2FIX(OS_VMS)
OS_UNIX =

From GzipFile#os_code - 0x03

INT2FIX(OS_UNIX)
OS_ATARI =

From GzipFile#os_code - 0x05

INT2FIX(OS_ATARI)
OS_OS2 =

From GzipFile#os_code - 0x06

INT2FIX(OS_OS2)
OS_MACOS =

From GzipFile#os_code - 0x07

INT2FIX(OS_MACOS)
OS_TOPS20 =

From GzipFile#os_code - 0x0a

INT2FIX(OS_TOPS20)
OS_WIN32 =

From GzipFile#os_code - 0x0b

INT2FIX(OS_WIN32)
OS_VMCMS =

From GzipFile#os_code - 0x04

INT2FIX(OS_VMCMS)
OS_ZSYSTEM =

From GzipFile#os_code - 0x08

INT2FIX(OS_ZSYSTEM)
OS_CPM =

From GzipFile#os_code - 0x09

INT2FIX(OS_CPM)
OS_QDOS =

From GzipFile#os_code - 0x0c

INT2FIX(OS_QDOS)
OS_RISCOS =

From GzipFile#os_code - 0x0d

INT2FIX(OS_RISCOS)
OS_UNKNOWN =

From GzipFile#os_code - 0xff

INT2FIX(OS_UNKNOWN)

Class Method Summary collapse

Class Method Details

.adler32Object

call-seq: Zlib.adler32(string, adler)

Calculates Adler-32 checksum for string, and returns updated value of adler. If string is omitted, it returns the Adler-32 initial value. If adler is omitted, it assumes that the initial value is given to adler.

FIXME: expression.



394
395
396
397
398
# File 'zlib.c', line 394

static VALUE
rb_zlib_adler32(int argc, VALUE *argv, VALUE klass)
{
    return do_checksum(argc, argv, adler32);
}

.adler32_combineObject

call-seq: Zlib.adler32_combine(adler1, adler2, len2)

Combine two Adler-32 check values in to one. alder1 is the first Adler-32 value, adler2 is the second Adler-32 value. len2 is the length of the string used to generate adler2.



411
412
413
414
415
416
# File 'zlib.c', line 411

static VALUE
rb_zlib_adler32_combine(VALUE klass, VALUE adler1, VALUE adler2, VALUE len2)
{
  return ULONG2NUM(
	adler32_combine(NUM2ULONG(adler1), NUM2ULONG(adler2), NUM2LONG(len2)));
}

.crc32Object

call-seq: Zlib.crc32(string, adler)

Calculates CRC checksum for string, and returns updated value of crc. If string is omitted, it returns the CRC initial value. If crc is omitted, it assumes that the initial value is given to crc.

FIXME: expression.



432
433
434
435
436
# File 'zlib.c', line 432

static VALUE
rb_zlib_crc32(int argc, VALUE *argv, VALUE klass)
{
    return do_checksum(argc, argv, crc32);
}

.crc32_combineObject

call-seq: Zlib.crc32_combine(crc1, crc2, len2)

Combine two CRC-32 check values in to one. crc1 is the first CRC-32 value, crc2 is the second CRC-32 value. len2 is the length of the string used to generate crc2.



449
450
451
452
453
454
# File 'zlib.c', line 449

static VALUE
rb_zlib_crc32_combine(VALUE klass, VALUE crc1, VALUE crc2, VALUE len2)
{
  return ULONG2NUM(
	crc32_combine(NUM2ULONG(crc1), NUM2ULONG(crc2), NUM2LONG(len2)));
}

.crc_tableObject

Returns the table for calculating CRC checksum as an array.



464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'zlib.c', line 464

static VALUE
rb_zlib_crc_table(VALUE obj)
{
    const unsigned long *crctbl;
    VALUE dst;
    int i;

    crctbl = get_crc_table();
    dst = rb_ary_new2(256);

    for (i = 0; i < 256; i++) {
	rb_ary_push(dst, rb_uint2inum(crctbl[i]));
    }
    return dst;
}

.deflateObject

call-seq: Zlib.deflate(string[, level])

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

Compresses the given string. Valid values of level are NO_COMPRESSION, BEST_SPEED, BEST_COMPRESSION, DEFAULT_COMPRESSION, and an integer from 0 to 9 (the default is 6).

This method is almost equivalent to the following code:

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

See also Zlib.inflate



1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
# File 'zlib.c', line 1381

static VALUE
rb_deflate_s_deflate(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;
}

.inflateObject

call-seq: Zlib::Inflate.inflate(string)

Decompresses string. Raises a Zlib::NeedDict exception if a preset dictionary is needed for decompression.

This method is almost equivalent to the following code:

def inflate(string)
  zstream = Zlib::Inflate.new
  buf = zstream.inflate(string)
  zstream.finish
  zstream.close
  buf
end

See also Zlib.deflate



1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
# File 'zlib.c', line 1704

static VALUE
rb_inflate_s_inflate(VALUE obj, VALUE src)
{
    struct zstream z;
    VALUE dst, args[2];
    int err;

    StringValue(src);
    zstream_init_inflate(&z);
    err = inflateInit(&z.stream);
    if (err != Z_OK) {
	raise_zlib_error(err, z.stream.msg);
    }
    ZSTREAM_READY(&z);

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

    OBJ_INFECT(dst, src);
    return dst;
}

.zlib_versionObject

Returns the string which represents the version of zlib library.



324
325
326
327
328
329
330
331
332
# File 'zlib.c', line 324

static VALUE
rb_zlib_version(VALUE klass)
{
    VALUE str;

    str = rb_str_new2(zlibVersion());
    OBJ_TAINT(str);  /* for safe */
    return str;
}