Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The accepted answer didn't work for me. Here's my modified version. Notice the different usage of <code>gz.unused</code>.</p> <p>Also, you should call <code>finish</code> on the <code>GzipReader</code> instance to avoid memory leaks.</p> <pre><code># gzcat-test.rb require 'zlib' require 'stringio' require 'digest/sha1' # gzip -c /usr/share/dict/web2 /usr/share/dict/web2a &gt; web-cat.gz io = File.open('web-cat.gz') # or, if you don't care about memory usage: # io = StringIO.new File.read 'web-cat.gz' # these will be hashes: {orig_name: 'filename', data_arr: unpacked_lines} entries=[] loop do entries &lt;&lt; {data_arr: []} # create a reader starting at io's current position gz = Zlib::GzipReader.new(io) entries.last[:orig_name] = gz.orig_name gz.each {|l| entries.last[:data_arr] &lt;&lt; l } unused = gz.unused # save this before calling #finish gz.finish if unused # Unused is not the entire remainder, but only part of it. # We need to back up since we've moved past the start of the next entry. io.pos -= unused.size else break end end io.close # verify the data entries.each do |entry_hash| p entry_hash[:orig_name] puts Digest::SHA1.hexdigest(entry_hash[:data_arr].join) end </code></pre> <p>Run:</p> <pre><code>&gt; ./gzcat-test.rb web2" a62edf8685920f7d5a95113020631cdebd18a185 "web2a" b0870457df2b8cae06a88657a198d9b52f8e2b0a </code></pre> <p>Our unpacked contents match the originals:</p> <pre><code>&gt; shasum /usr/share/dict/web* a62edf8685920f7d5a95113020631cdebd18a185 /usr/share/dict/web2 b0870457df2b8cae06a88657a198d9b52f8e2b0a /usr/share/dict/web2a </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload