123456789_123456789_123456789_123456789_123456789_

Class: Zlib::Inflate

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ZStream
Instance Chain:
self, ZStream
Inherits: Zlib::ZStream
Defined in: ext/zlib/zlib.c,
ext/zlib/zlib.c

Overview

Zlib:Inflate is the class for decompressing compressed data. Unlike Deflate, an instance of this class is not able to duplicate (clone, dup) itself.

Class Method Summary

Instance Attribute Summary

ZStream - Inherited

#avail_out

Returns number of bytes of free spaces in output buffer.

#avail_out=

Allocates size bytes of free space in the output buffer.

#closed?

Alias for ZStream#ended?.

#ended?

Returns true if the stream is closed.

#finished?

Returns true if the stream is finished.

#stream_end?

Instance Method Summary

ZStream - Inherited

#adler

Returns the adler-32 checksum.

#avail_in

Returns bytes of data in the input buffer.

#close

Alias for ZStream#end.

#data_type

Guesses the type of the data which have been inputed into the stream.

#end

Closes the stream.

#finish

Finishes the stream and flushes output buffer.

#flush_next_in,
#flush_next_out

Flushes output buffer and returns all data in that buffer.

#reset

Resets and initializes the stream.

#total_in

Returns the total bytes of the input data to the stream.

#total_out

Returns the total bytes of the output data from the stream.

Constructor Details

.new(window_bits = Zlib::MAX_WBITS)

Creates a new inflate stream for decompression. window_bits sets the size of the history buffer and can have the following values:

0

Have inflate use the window size from the zlib header of the compressed stream.

(8..15)

Overrides the window size of the inflate header in the compressed stream. The window size must be greater than or equal to the window size of the compressed stream.

Greater than 15

Add 32 to window_bits to enable zlib and gzip decoding with automatic header detection, or add 16 to decode only the gzip format (a Zlib::DataError will be raised for a non-gzip stream).

(-8..-15)

Enables raw deflate mode which will not generate a check value, and will not look for any check values for comparison at the end of the stream.

This is for use with other formats that use the deflate compressed data format such as zip which provide their own check values.

Example

open "compressed.file" do |compressed_io|
  zi = Zlib::Inflate.new(Zlib::MAX_WBITS + 32)

  begin
    open "uncompressed.file", "w+" do |uncompressed_io|
      uncompressed_io << zi.inflate(compressed_io.read)
    end
  ensure
    zi.close
  end
end

Class Method Details

Zlib.inflate(string) .inflate(string)

Decompresses string. Raises a 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

Instance Attribute Details

#sync_point?Boolean (readonly)

Quoted verbatim from original documentation:

What is this?

:)

Instance Method Details

#<<(string)

Inputs string into the inflate stream just like #inflate, but returns the Inflate object itself. The output from the stream is preserved in output buffer.

#add_dictionary(string)

Provide the inflate stream with a dictionary that may be required in the future. Multiple dictionaries may be provided. The inflate stream will automatically choose the correct user-provided dictionary based on the stream's required dictionary.

#inflate(deflate_string) ⇒ String #inflate(deflate_string) {|chunk| ... } ⇒ nil

Inputs deflate_string into the inflate stream and returns the output from the stream. Calling this method, both the input and the output buffer of the stream are flushed. If string is nil, this method finishes the stream, just like ZStream#finish.

If a block is given consecutive inflated chunks from the deflate_string are yielded to the block and nil is returned.

Raises a NeedDict exception if a preset dictionary is needed to decompress. Set the dictionary by #set_dictionary and then call this method again with an empty string to flush the stream:

inflater = Zlib::Inflate.new

begin
  out = inflater.inflate compressed
rescue Zlib::NeedDict
  # ensure the dictionary matches the stream's required dictionary
  raise unless inflater.adler == Zlib.adler32(dictionary)

  inflater.set_dictionary dictionary
  inflater.inflate ''
end

# ...

inflater.close

See also .new

#set_dictionary(dic)

Sets the preset dictionary and returns string. This method is available just only after a NeedDict exception was raised. See zlib.h for details.

#sync(string)

Inputs string into the end of input buffer and skips data until a full flush point can be found. If the point is found in the buffer, this method flushes the buffer and returns false. Otherwise it returns true and the following data of full flush point is preserved in the buffer.