Class: Zlib::Inflate
- Defined in:
- ext/zlib/zlib.c,
ext/zlib/zlib.c more...
Overview
Zlib:Inflate is the class for decompressing compressed data. Unlike Zlib::Deflate, an instance of this class is not able to duplicate (clone, dup) itself.
Class Method Summary collapse
-
.inflate(src) ⇒ Object
call-seq: Zlib.inflate(string) Zlib::Inflate.inflate(string).
Instance Method Summary collapse
-
#<<(string) ⇒ Object
Inputs
string
into the inflate stream just like Zlib::Inflate#inflate, but returns the Zlib::Inflate object itself. -
#add_dictionary(dictionary) ⇒ Object
call-seq: add_dictionary(string).
-
#inflate(*args) ⇒ Object
call-seq: inflate(deflate_string, buffer: nil) -> String inflate(deflate_string, buffer: nil) { |chunk| … } -> nil.
-
#initialize(*args) ⇒ Object
constructor
call-seq: Zlib::Inflate.new(window_bits = Zlib::MAX_WBITS).
-
#set_dictionary(dic) ⇒ Object
Sets the preset dictionary and returns
string
. -
#sync(string) ⇒ Object
Inputs
string
into the end of input buffer and skips data until a full flush point can be found. -
#sync_point? ⇒ Boolean
Quoted verbatim from original documentation:.
Methods inherited from ZStream
#adler, #avail_in, #avail_out, #avail_out=, #close, #closed?, #data_type, #end, #ended?, #finish, #finished?, #flush_next_in, #flush_next_out, #reset, #stream_end?, #total_in, #total_out
Constructor Details
permalink #initialize(*args) ⇒ Object
call-seq:
Zlib::Inflate.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
2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 |
# File 'ext/zlib/zlib.c', line 2042
static VALUE
rb_inflate_initialize(int argc, VALUE *argv, VALUE obj)
{
struct zstream *z;
VALUE wbits;
int err;
rb_scan_args(argc, argv, "01", &wbits);
TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z);
err = inflateInit2(&z->stream, ARG_WBITS(wbits));
if (err != Z_OK) {
raise_zlib_error(err, z->stream.msg);
}
ZSTREAM_READY(z);
return obj;
}
|
Class Method Details
permalink .inflate(src) ⇒ Object
call-seq:
Zlib.inflate(string)
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
2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 |
# File 'ext/zlib/zlib.c', line 2095
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_ensure_end, (VALUE)&z);
return dst;
}
|
Instance Method Details
permalink #<<(string) ⇒ Object
Inputs string
into the inflate stream just like Zlib::Inflate#inflate, but returns the Zlib::Inflate object itself. The output from the stream is preserved in output buffer.
2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 |
# File 'ext/zlib/zlib.c', line 2278
static VALUE
rb_inflate_addstr(VALUE obj, VALUE src)
{
struct zstream *z = get_zstream(obj);
if (ZSTREAM_IS_FINISHED(z)) {
if (!NIL_P(src)) {
StringValue(src);
zstream_append_buffer2(z, src);
}
}
else {
do_inflate(z, src);
if (ZSTREAM_IS_FINISHED(z)) {
zstream_passthrough_input(z);
}
}
return obj;
}
|
permalink #add_dictionary(dictionary) ⇒ Object
call-seq: 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.
2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 |
# File 'ext/zlib/zlib.c', line 2139
static VALUE
rb_inflate_add_dictionary(VALUE obj, VALUE dictionary)
{
VALUE dictionaries = rb_ivar_get(obj, id_dictionaries);
VALUE checksum = do_checksum(1, &dictionary, adler32);
rb_hash_aset(dictionaries, checksum, dictionary);
return obj;
}
|
permalink #inflate(*args) ⇒ Object
call-seq:
inflate(deflate_string, buffer: nil) -> String
inflate(deflate_string, buffer: nil) { |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 Zlib::ZStream#finish.
If a block is given consecutive inflated chunks from the deflate_string
are yielded to the block and nil
is returned.
If a :buffer keyword argument is given and not nil:
-
The :buffer keyword should be a String, and will used as the output buffer. Using this option can reuse the memory required during inflation.
-
When not passing a block, the return value will be the same object as the :buffer keyword argument.
-
When passing a block, the yielded chunks will be the same value as the :buffer keyword argument.
Raises a Zlib::NeedDict exception if a preset dictionary is needed to decompress. Set the dictionary by Zlib::Inflate#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 Zlib::Inflate.new
2263 2264 2265 2266 2267 2268 2269 |
# File 'ext/zlib/zlib.c', line 2263
static VALUE
rb_inflate_inflate(int argc, VALUE* argv, VALUE obj)
{
struct zstream *z = get_zstream(obj);
struct rb_zlib_inflate_arguments arguments = {z, argc, argv};
return rb_mutex_synchronize(z->mutex, rb_inflate_inflate_body, (VALUE)&arguments);
}
|
permalink #set_dictionary(dic) ⇒ Object
Sets the preset dictionary and returns string
. This method is available just only after a Zlib::NeedDict exception was raised. See zlib.h for details.
2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 |
# File 'ext/zlib/zlib.c', line 2346
static VALUE
rb_inflate_set_dictionary(VALUE obj, VALUE dic)
{
struct zstream *z = get_zstream(obj);
VALUE src = dic;
int err;
StringValue(src);
err = inflateSetDictionary(&z->stream,
(Bytef*)RSTRING_PTR(src), RSTRING_LENINT(src));
if (err != Z_OK) {
raise_zlib_error(err, z->stream.msg);
}
return dic;
}
|
permalink #sync(string) ⇒ Object
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.
2307 2308 2309 2310 2311 2312 2313 2314 |
# File 'ext/zlib/zlib.c', line 2307
static VALUE
rb_inflate_sync(VALUE obj, VALUE src)
{
struct zstream *z = get_zstream(obj);
StringValue(src);
return zstream_sync(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src));
}
|
permalink #sync_point? ⇒ Boolean
Quoted verbatim from original documentation:
What is this?
:)
2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 |
# File 'ext/zlib/zlib.c', line 2323
static VALUE
rb_inflate_sync_point_p(VALUE obj)
{
struct zstream *z = get_zstream(obj);
int err;
err = inflateSyncPoint(&z->stream);
if (err == 1) {
return Qtrue;
}
if (err != Z_OK) {
raise_zlib_error(err, z->stream.msg);
}
return Qfalse;
}
|