File: C:/Ruby27-x64/share/doc/ruby/html/Zlib/Inflate.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>class Zlib::Inflate - RDoc Documentation</title>
<script type="text/javascript">
var rdoc_rel_prefix = "../";
var index_rel_prefix = "../";
</script>
<script src="../js/navigation.js" defer></script>
<script src="../js/search.js" defer></script>
<script src="../js/search_index.js" defer></script>
<script src="../js/searcher.js" defer></script>
<script src="../js/darkfish.js" defer></script>
<link href="../css/fonts.css" rel="stylesheet">
<link href="../css/rdoc.css" rel="stylesheet">
<body id="top" role="document" class="class">
<nav role="navigation">
<div id="project-navigation">
<div id="home-section" role="region" title="Quick navigation" class="nav-section">
<h2>
<a href="../index.html" rel="home">Home</a>
</h2>
<div id="table-of-contents-navigation">
<a href="../table_of_contents.html#pages">Pages</a>
<a href="../table_of_contents.html#classes">Classes</a>
<a href="../table_of_contents.html#methods">Methods</a>
</div>
</div>
<div id="search-section" role="search" class="project-section initially-hidden">
<form action="#" method="get" accept-charset="utf-8">
<div id="search-field-wrapper">
<input id="search-field" role="combobox" aria-label="Search"
aria-autocomplete="list" aria-controls="search-results"
type="text" name="search" placeholder="Search" spellcheck="false"
title="Type to search, Up and Down to navigate, Enter to load">
</div>
<ul id="search-results" aria-label="Search Results"
aria-busy="false" aria-expanded="false"
aria-atomic="false" class="initially-hidden"></ul>
</form>
</div>
</div>
<div id="class-metadata">
<div id="parent-class-section" class="nav-section">
<h3>Parent</h3>
<p class="link"><a href="ZStream.html">Zlib::ZStream</a>
</div>
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
<h3>Methods</h3>
<ul class="link-list" role="directory">
<li ><a href="#method-c-inflate">::inflate</a>
<li ><a href="#method-c-new">::new</a>
<li ><a href="#method-i-3C-3C">#<<</a>
<li ><a href="#method-i-add_dictionary">#add_dictionary</a>
<li ><a href="#method-i-inflate">#inflate</a>
<li ><a href="#method-i-set_dictionary">#set_dictionary</a>
<li ><a href="#method-i-sync">#sync</a>
<li ><a href="#method-i-sync_point-3F">#sync_point?</a>
</ul>
</div>
</div>
</nav>
<main role="main" aria-labelledby="class-Zlib::Inflate">
<h1 id="class-Zlib::Inflate" class="class">
class Zlib::Inflate
</h1>
<section class="description">
<p>Zlib:Inflate is the class for decompressing compressed data. Unlike <a href="Deflate.html"><code>Zlib::Deflate</code></a>, an instance of this class is not able to duplicate (clone, dup) itself.</p>
</section>
<section id="5Buntitled-5D" class="documentation-section">
<section id="public-class-5Buntitled-5D-method-details" class="method-section">
<header>
<h3>Public Class Methods</h3>
</header>
<div id="method-c-inflate" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
inflate(string)
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
Zlib::Inflate.inflate(string)
</span>
</div>
<div class="method-description">
<p>Decompresses <code>string</code>. Raises a <a href="NeedDict.html"><code>Zlib::NeedDict</code></a> exception if a preset dictionary is needed for decompression.</p>
<p>This method is almost equivalent to the following code:</p>
<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">inflate</span>(<span class="ruby-identifier">string</span>)
<span class="ruby-identifier">zstream</span> = <span class="ruby-constant">Zlib</span><span class="ruby-operator">::</span><span class="ruby-constant">Inflate</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">buf</span> = <span class="ruby-identifier">zstream</span>.<span class="ruby-identifier">inflate</span>(<span class="ruby-identifier">string</span>)
<span class="ruby-identifier">zstream</span>.<span class="ruby-identifier">finish</span>
<span class="ruby-identifier">zstream</span>.<span class="ruby-identifier">close</span>
<span class="ruby-identifier">buf</span>
<span class="ruby-keyword">end</span>
</pre>
<p>See also <a href="../Zlib.html#method-c-deflate"><code>Zlib.deflate</code></a></p>
<div class="method-source-code" id="inflate-source">
<pre>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;
}</pre>
</div>
</div>
</div>
<div id="method-c-new" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
Zlib::Inflate.new(window_bits = Zlib::MAX_WBITS)
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Creates a new inflate stream for decompression. <code>window_bits</code> sets the size of the history buffer and can have the following values:</p>
<dl class="rdoc-list note-list"><dt>0
<dd>
<p>Have inflate use the window size from the zlib header of the compressed stream.</p>
</dd><dt>(8..15)
<dd>
<p>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.</p>
</dd><dt>Greater than 15
<dd>
<p>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 <a href="DataError.html"><code>Zlib::DataError</code></a> will be raised for a non-gzip stream).</p>
</dd><dt>(-8..-15)
<dd>
<p>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.</p>
<p>This is for use with other formats that use the deflate compressed data format such as zip which provide their own check values.</p>
</dd></dl>
<h2 id="method-c-new-label-Example">Example<span><a href="#method-c-new-label-Example">¶</a> <a href="#top">↑</a></span></h2>
<pre class="ruby"><span class="ruby-identifier">open</span> <span class="ruby-string">"compressed.file"</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">compressed_io</span><span class="ruby-operator">|</span>
<span class="ruby-identifier">zi</span> = <span class="ruby-constant">Zlib</span><span class="ruby-operator">::</span><span class="ruby-constant">Inflate</span>.<span class="ruby-identifier">new</span>(<span class="ruby-constant">Zlib</span><span class="ruby-operator">::</span><span class="ruby-constant">MAX_WBITS</span> <span class="ruby-operator">+</span> <span class="ruby-value">32</span>)
<span class="ruby-keyword">begin</span>
<span class="ruby-identifier">open</span> <span class="ruby-string">"uncompressed.file"</span>, <span class="ruby-string">"w+"</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">uncompressed_io</span><span class="ruby-operator">|</span>
<span class="ruby-identifier">uncompressed_io</span> <span class="ruby-operator"><<</span> <span class="ruby-identifier">zi</span>.<span class="ruby-identifier">inflate</span>(<span class="ruby-identifier">compressed_io</span>.<span class="ruby-identifier">read</span>)
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">ensure</span>
<span class="ruby-identifier">zi</span>.<span class="ruby-identifier">close</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>
<div class="method-source-code" id="new-source">
<pre>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;
}</pre>
</div>
</div>
</div>
</section>
<section id="public-instance-5Buntitled-5D-method-details" class="method-section">
<header>
<h3>Public Instance Methods</h3>
</header>
<div id="method-i-3C-3C" class="method-detail ">
<div class="method-heading">
<span class="method-name"><<</span><span
class="method-args">(p1)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Same as <a href="../IO.html"><code>IO</code></a>.</p>
<div class="method-source-code" id="3C-3C-source">
<pre>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;
}</pre>
</div>
</div>
</div>
<div id="method-i-add_dictionary" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
add_dictionary(string)
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>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.</p>
<div class="method-source-code" id="add_dictionary-source">
<pre>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;
}</pre>
</div>
</div>
</div>
<div id="method-i-inflate" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
inflate(deflate_string) → String
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
inflate(deflate_string) { |chunk| ... } → nil
</span>
</div>
<div class="method-description">
<p>Inputs <code>deflate_string</code> 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 <code>nil</code>, this method finishes the stream, just like <a href="ZStream.html#method-i-finish"><code>Zlib::ZStream#finish</code></a>.</p>
<p>If a block is given consecutive inflated chunks from the <code>deflate_string</code> are yielded to the block and <code>nil</code> is returned.</p>
<p>Raises a <a href="NeedDict.html"><code>Zlib::NeedDict</code></a> exception if a preset dictionary is needed to decompress. <a href="../Set.html"><code>Set</code></a> the dictionary by <a href="Inflate.html#method-i-set_dictionary"><code>Zlib::Inflate#set_dictionary</code></a> and then call this method again with an empty string to flush the stream:</p>
<pre class="ruby"><span class="ruby-identifier">inflater</span> = <span class="ruby-constant">Zlib</span><span class="ruby-operator">::</span><span class="ruby-constant">Inflate</span>.<span class="ruby-identifier">new</span>
<span class="ruby-keyword">begin</span>
<span class="ruby-identifier">out</span> = <span class="ruby-identifier">inflater</span>.<span class="ruby-identifier">inflate</span> <span class="ruby-identifier">compressed</span>
<span class="ruby-keyword">rescue</span> <span class="ruby-constant">Zlib</span><span class="ruby-operator">::</span><span class="ruby-constant">NeedDict</span>
<span class="ruby-comment"># ensure the dictionary matches the stream's required dictionary</span>
<span class="ruby-identifier">raise</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">inflater</span>.<span class="ruby-identifier">adler</span> <span class="ruby-operator">==</span> <span class="ruby-constant">Zlib</span>.<span class="ruby-identifier">adler32</span>(<span class="ruby-identifier">dictionary</span>)
<span class="ruby-identifier">inflater</span>.<span class="ruby-identifier">set_dictionary</span> <span class="ruby-identifier">dictionary</span>
<span class="ruby-identifier">inflater</span>.<span class="ruby-identifier">inflate</span> <span class="ruby-string">''</span>
<span class="ruby-keyword">end</span>
<span class="ruby-comment"># ...</span>
<span class="ruby-identifier">inflater</span>.<span class="ruby-identifier">close</span>
</pre>
<p>See also <a href="Inflate.html#method-c-new"><code>Zlib::Inflate.new</code></a></p>
<div class="method-source-code" id="inflate-source">
<pre>static VALUE
rb_inflate_inflate(VALUE obj, VALUE src)
{
struct zstream *z = get_zstream(obj);
VALUE dst;
if (ZSTREAM_IS_FINISHED(z)) {
if (NIL_P(src)) {
dst = zstream_detach_buffer(z);
}
else {
StringValue(src);
zstream_append_buffer2(z, src);
dst = rb_str_new(0, 0);
}
}
else {
do_inflate(z, src);
dst = zstream_detach_buffer(z);
if (ZSTREAM_IS_FINISHED(z)) {
zstream_passthrough_input(z);
}
}
return dst;
}</pre>
</div>
</div>
</div>
<div id="method-i-set_dictionary" class="method-detail ">
<div class="method-heading">
<span class="method-name">set_dictionary</span><span
class="method-args">(p1)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Sets the preset dictionary and returns <code>string</code>. This method is available just only after a <a href="NeedDict.html"><code>Zlib::NeedDict</code></a> exception was raised. See zlib.h for details.</p>
<div class="method-source-code" id="set_dictionary-source">
<pre>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;
}</pre>
</div>
</div>
</div>
<div id="method-i-sync" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
sync(string)
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Inputs <code>string</code> 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 <code>true</code> and the following data of full flush point is preserved in the buffer.</p>
<div class="method-source-code" id="sync-source">
<pre>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));
}</pre>
</div>
</div>
</div>
<div id="method-i-sync_point-3F" class="method-detail ">
<div class="method-heading">
<span class="method-name">sync_point?</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Quoted verbatim from original documentation:</p>
<pre class="ruby"><span class="ruby-constant">What</span> <span class="ruby-identifier">is</span> <span class="ruby-identifier">this?</span>
</pre>
<p><code>:)</code></p>
<div class="method-source-code" id="sync_point-3F-source">
<pre>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;
}</pre>
</div>
</div>
</div>
</section>
</section>
</main>
<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.2.1.1.
<p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>