Module: Zlib
| Relationships & Source Files | |
| Namespace Children | |
| Classes: | |
| Exceptions: | |
| Defined in: | ext/zlib/zlib.c | 
Overview
This module provides access to the zlib library. Zlib is designed to be a portable, free, general-purpose, legally unencumbered – that is, not covered by any patents – lossless data-compression library for use on virtually any computer hardware and operating system.
The zlib compression library provides in-memory compression and decompression functions, including integrity checks of the uncompressed data.
The zlib compressed data format is described in RFC 1950, which is a wrapper around a deflate stream which is described in RFC 1951.
The library also supports reading and writing files in gzip (.gz) format with an interface similar to that of IO. The gzip format is described in RFC 1952 which is also a wrapper around a deflate stream.
The zlib format was designed to be compact and fast for use in memory and on communications channels. The gzip format was designed for single-file compression on file systems, has a larger header than zlib to maintain directory information, and uses a different, slower check method than zlib.
See your system's zlib.h for further information about zlib
Sample usage
Using the wrapper to compress strings with default parameters is quite simple:
require "zlib"
data_to_compress = File.read("don_quixote.txt")
puts "Input size: #{data_to_compress.size}"
#=> Input size: 2347740
data_compressed = Zlib::Deflate.deflate(data_to_compress)
puts "Compressed size: #{data_compressed.size}"
#=> Compressed size: 887238
uncompressed_data = Zlib::Inflate.inflate(data_compressed)
puts "Uncompressed data is: #{uncompressed_data}"
#=> Uncompressed data is: The Project Gutenberg EBook of Don Quixote...Class tree
- 
- 
Zlib::StreamEnd 
- 
Zlib::NeedDict 
- 
Zlib::DataError 
- 
Zlib::StreamError 
- 
Zlib::MemError 
- 
Zlib::BufError 
- 
Zlib::VersionError 
 
- 
(if you have GZIP_SUPPORT)
- 
- 
Zlib::GzipFile::LengthError 
- 
Zlib::GzipFile::CRCError 
- 
Zlib::GzipFile::NoFooter 
 
- 
Constant Summary
- 
    ASCII =
    # File 'ext/zlib/zlib.c', line 4302The underlying constant Z_ASCII was deprecated in favor of Z_TEXT in zlib 1.2.2. New applications should not use this constant. See Zlib::Deflate#data_type.Represents text data as guessed by deflate. NOTE 
- 
    BEST_COMPRESSION =
    # File 'ext/zlib/zlib.c', line 4347Slowest compression level, but with the best space savings. INT2FIX(Z_BEST_COMPRESSION) 
- 
    BEST_SPEED =
    # File 'ext/zlib/zlib.c', line 4345Fastest compression level, but with with lowest space savings. INT2FIX(Z_BEST_SPEED) 
- 
    BINARY =
    # File 'ext/zlib/zlib.c', line 4294:Deflate#data_type. Represents binary data as guessed by deflate. See Zlib 
- 
    DEFAULT_COMPRESSION =
    # File 'ext/zlib/zlib.c', line 4351Default compression level which is a good trade-off between space and time INT2FIX(Z_DEFAULT_COMPRESSION) 
- 
    DEFAULT_STRATEGY =
    # File 'ext/zlib/zlib.c', line 4380Default deflate strategy which is used for normal data. INT2FIX(Z_DEFAULT_STRATEGY) 
- 
    DEF_MEM_LEVEL =
    # File 'ext/zlib/zlib.c', line 4390The default memory level for allocating zlib deflate compression state. INT2FIX(DEF_MEM_LEVEL) 
- 
    FILTERED =
    # File 'ext/zlib/zlib.c', line 4360::Zlib::Deflate strategy for data produced by a filter (or predictor). The effect of FILTEREDis to force more Huffman codes and less string matching; it is somewhat intermediate between DEFAULT_STRATEGY and HUFFMAN_ONLY. Filtered data consists mostly of small values with a somewhat random distribution.INT2FIX(Z_FILTERED) 
- 
    FINISH =
    # File 'ext/zlib/zlib.c', line 4418Processes all pending input and flushes pending output. INT2FIX(Z_FINISH) 
- 
    FIXED =
    # File 'ext/zlib/zlib.c', line 4376::Zlib::Deflate strategy which prevents the use of dynamic Huffman codes, allowing for a simpler decoder for specialized applications. INT2FIX(Z_FIXED) 
- 
    FULL_FLUSH =
    # File 'ext/zlib/zlib.c', line 4415Flushes all output as with SYNC_FLUSH, and the compression state is reset so that decompression can restart from this point if previous compressed data has been damaged or if random access is desired. Like SYNC_FLUSH, using FULL_FLUSHtoo often can seriously degrade compression.INT2FIX(Z_FULL_FLUSH) 
- 
    HUFFMAN_ONLY =
    # File 'ext/zlib/zlib.c', line 4363::Zlib::Deflate strategy which uses Huffman codes only (no string matching). INT2FIX(Z_HUFFMAN_ONLY) 
- 
    MAX_MEM_LEVEL =
    # File 'ext/zlib/zlib.c', line 4394The maximum memory level for allocating zlib deflate compression state. INT2FIX(MAX_MEM_LEVEL) 
- 
    MAX_WBITS =
    # File 'ext/zlib/zlib.c', line 4386:Inflate.new for details.The maximum size of the zlib history buffer. Note that zlib allows larger values to enable different inflate modes. See Zlib 
- 
    NO_COMPRESSION =
    # File 'ext/zlib/zlib.c', line 4343No compression, passes through data untouched. Use this for appending pre-compressed data to a deflate stream. INT2FIX(Z_NO_COMPRESSION) 
- 
    NO_FLUSH =
    # File 'ext/zlib/zlib.c', line 4400NO_FLUSHis the default flush method and allows deflate to decide how much data to accumulate before producing output in order to maximize compression.INT2FIX(Z_NO_FLUSH) 
- 
    OS_AMIGA =
    # File 'ext/zlib/zlib.c', line 4510OS code for Amiga hosts INT2FIX(OS_AMIGA) 
- 
    OS_ATARI =
    # File 'ext/zlib/zlib.c', line 4516OS code for Atari hosts INT2FIX(OS_ATARI) 
- 
    OS_CODE =
    # File 'ext/zlib/zlib.c', line 4506The OS code of current host INT2FIX(OS_CODE) 
- 
    OS_CPM =
    # File 'ext/zlib/zlib.c', line 4530OS code for CP/M hosts INT2FIX(OS_CPM) 
- 
    OS_MACOS =
    # File 'ext/zlib/zlib.c', line 4520OS code for Mac OS hosts INT2FIX(OS_MACOS) 
- 
    OS_MSDOS =
    # File 'ext/zlib/zlib.c', line 4508OS code for MSDOS hosts INT2FIX(OS_MSDOS) 
- 
    OS_OS2 =
    # File 'ext/zlib/zlib.c', line 4518OS code for OS2 hosts INT2FIX(OS_OS2) 
- 
    OS_QDOS =
    # File 'ext/zlib/zlib.c', line 4532OS code for QDOS hosts INT2FIX(OS_QDOS) 
- 
    OS_RISCOS =
    # File 'ext/zlib/zlib.c', line 4534OS code for RISC OS hosts INT2FIX(OS_RISCOS) 
- 
    OS_TOPS20 =
    # File 'ext/zlib/zlib.c', line 4522OS code for TOPS-20 hosts INT2FIX(OS_TOPS20) 
- 
    OS_UNIX =
    # File 'ext/zlib/zlib.c', line 4514OS code for UNIX hosts INT2FIX(OS_UNIX) 
- 
    OS_UNKNOWN =
    # File 'ext/zlib/zlib.c', line 4536OS code for unknown hosts INT2FIX(OS_UNKNOWN) 
- 
    OS_VMCMS =
    # File 'ext/zlib/zlib.c', line 4526OS code for VM OS hosts INT2FIX(OS_VMCMS) 
- 
    OS_VMS =
    # File 'ext/zlib/zlib.c', line 4512OS code for VMS hosts INT2FIX(OS_VMS) 
- 
    OS_WIN32 =
    # File 'ext/zlib/zlib.c', line 4524OS code for Win32 hosts INT2FIX(OS_WIN32) 
- 
    OS_ZSYSTEM =
    # File 'ext/zlib/zlib.c', line 4528OS code for Z-System hosts INT2FIX(OS_ZSYSTEM) 
- 
    RLE =
    # File 'ext/zlib/zlib.c', line 4369::Zlib::Deflate compression strategy designed to be almost as fast as HUFFMAN_ONLY, but give better compression for PNG image data. INT2FIX(Z_RLE) 
- 
    SYNC_FLUSH =
    # File 'ext/zlib/zlib.c', line 4407The SYNC_FLUSH method flushes all pending output to the output buffer and the output is aligned on a byte boundary. Flushing may degrade compression so it should be used only when necessary, such as at a request or response boundary for a network stream. INT2FIX(Z_SYNC_FLUSH) 
- 
    TEXT =
    # File 'ext/zlib/zlib.c', line 4308:Deflate#data_type. Represents text data as guessed by deflate. See Zlib 
- 
    UNKNOWN =
    # File 'ext/zlib/zlib.c', line 4314:Deflate#data_type. Represents an unknown data type as guessed by deflate. See Zlib 
- 
    VERSION =
    # File 'ext/zlib/zlib.c', line 4267The Ruby/zlib version string. rb_str_new2(RUBY_ZLIB_VERSION) 
- 
    ZLIB_VERSION =
    # File 'ext/zlib/zlib.c', line 4269The string which represents the version of zlib.h rb_str_new2(ZLIB_VERSION) 
Class Method Summary
- 
    
      .deflate(string[, level])  
    
    Alias for Deflate.deflate. 
- 
    
      .inflate(string)  
    
    Alias for Inflate.inflate. 
- 
    
      .adler32(string, adler)  
    
    mod_func
    Calculates Adler-32 checksum for string, and returns updated value ofadler.
- 
    
      .adler32_combine(adler1, adler2, len2)  
    
    mod_func
    Combine two Adler-32 check values in to one. 
- 
    
      .crc32(string, crc)  
    
    mod_func
    Calculates CRC checksum for string, and returns updated value ofcrc.
- 
    
      .crc32_combine(crc1, crc2, len2)  
    
    mod_func
    Combine two CRC-32 check values in to one. 
- 
    
      .crc_table  
    
    mod_func
    Returns the table for calculating CRC checksum as an array. 
- 
    
      .zlib_version  
    
    mod_func
    Returns the string which represents the version of zlib library. 
Class Method Details
.adler32(string, adler) (mod_func)
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.
Example usage:
require "zlib"
data = "foo"
puts "Adler32 checksum: #{Zlib.adler32(data).to_s(16)}"
#=> Adler32 checksum: 2820145.adler32_combine(adler1, adler2, len2) (mod_func)
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.
.crc32(string, crc) (mod_func)
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.
.crc32_combine(crc1, crc2, len2) (mod_func)
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.
.crc_table (mod_func)
Returns the table for calculating CRC checksum as an array.
.deflate(string[, level])
Alias for Deflate.deflate.
.inflate(string)
Alias for Inflate.inflate.
.zlib_version (mod_func)
Returns the string which represents the version of zlib library.