File: inflater.rb

package info (click to toggle)
ruby-zip 3.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,120 kB
  • sloc: ruby: 9,958; makefile: 23
file content (57 lines) | stat: -rw-r--r-- 1,337 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# frozen_string_literal: true

module Zip
  class Inflater < Decompressor # :nodoc:all
    def initialize(*args)
      super

      @buffer = +''
      @zlib_inflater = ::Zlib::Inflate.new(-Zlib::MAX_WBITS)
    end

    def read(length = nil, outbuf = +'')
      return (length.nil? || length.zero? ? '' : nil) if eof?

      while length.nil? || (@buffer.bytesize < length)
        break if input_finished?

        @buffer << produce_input
      end

      outbuf.replace(@buffer.slice!(0...(length || @buffer.bytesize)))
    end

    def eof?
      @buffer.empty? && input_finished?
    end

    # Alias for compatibility. Remove for version 4.
    alias eof eof?

    private

    def produce_input
      retried = 0
      begin
        @zlib_inflater.inflate(input_stream.read(Decompressor::CHUNK_SIZE))
      rescue Zlib::BufError
        raise if retried >= 5 # how many times should we retry?

        retried += 1
        retry
      end
    rescue Zlib::Error => e
      raise ::Zip::DecompressionError, e
    end

    def input_finished?
      @zlib_inflater.finished?
    end
  end

  ::Zip::Decompressor.register(::Zip::COMPRESSION_METHOD_DEFLATE, ::Zip::Inflater)
end

# Copyright (C) 2002, 2003 Thomas Sondergaard
# rubyzip is free software; you can redistribute it and/or
# modify it under the terms of the ruby license.