2010-11-30 16:27:59 +08:00
|
|
|
module Zip
|
2013-06-03 02:33:03 +08:00
|
|
|
class Inflater < Decompressor #:nodoc:all
|
2019-12-21 18:02:01 +08:00
|
|
|
def initialize(input_stream)
|
2014-12-31 00:03:17 +08:00
|
|
|
super(input_stream)
|
2013-06-03 02:33:03 +08:00
|
|
|
@zlib_inflater = ::Zlib::Inflate.new(-Zlib::MAX_WBITS)
|
2019-02-28 00:23:29 +08:00
|
|
|
@output_buffer = ''.dup
|
2013-06-03 02:33:03 +08:00
|
|
|
@has_returned_empty_string = false
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
2011-11-18 04:53:04 +08:00
|
|
|
|
2013-08-30 05:22:19 +08:00
|
|
|
def sysread(number_of_bytes = nil, buf = '')
|
2013-06-03 02:33:03 +08:00
|
|
|
readEverything = number_of_bytes.nil?
|
2014-07-02 19:05:13 +08:00
|
|
|
while readEverything || @output_buffer.bytesize < number_of_bytes
|
2011-11-18 04:53:04 +08:00
|
|
|
break if internal_input_finished?
|
2014-07-02 19:05:13 +08:00
|
|
|
@output_buffer << internal_produce_input(buf)
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
2019-12-23 23:54:56 +08:00
|
|
|
return value_when_finished if @output_buffer.bytesize == 0 && eof?
|
2014-07-02 19:05:13 +08:00
|
|
|
end_index = number_of_bytes.nil? ? @output_buffer.bytesize : number_of_bytes
|
|
|
|
@output_buffer.slice!(0...end_index)
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
2011-11-18 04:53:04 +08:00
|
|
|
|
2019-12-23 23:54:56 +08:00
|
|
|
def eof
|
2013-06-03 02:33:03 +08:00
|
|
|
@output_buffer.empty? && internal_input_finished?
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
2013-06-03 02:33:03 +08:00
|
|
|
|
2019-12-23 23:54:56 +08:00
|
|
|
alias_method :eof?, :eof
|
2010-11-30 16:27:59 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2014-07-02 19:05:13 +08:00
|
|
|
def internal_produce_input(buf = '')
|
2010-11-30 16:27:59 +08:00
|
|
|
retried = 0
|
|
|
|
begin
|
2019-12-21 18:02:01 +08:00
|
|
|
@zlib_inflater.inflate(@input_stream.read(Decompressor::CHUNK_SIZE, buf))
|
2010-11-30 16:27:59 +08:00
|
|
|
rescue Zlib::BufError
|
2013-06-03 02:33:03 +08:00
|
|
|
raise if retried >= 5 # how many times should we retry?
|
2010-11-30 16:27:59 +08:00
|
|
|
retried += 1
|
|
|
|
retry
|
|
|
|
end
|
2019-12-29 21:43:14 +08:00
|
|
|
rescue Zlib::Error => e
|
|
|
|
raise(::Zip::DecompressionError, 'zlib error while inflating')
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def internal_input_finished?
|
2013-06-03 02:33:03 +08:00
|
|
|
@zlib_inflater.finished?
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
2014-07-02 19:05:13 +08:00
|
|
|
def value_when_finished # mimic behaviour of ruby File object.
|
|
|
|
return if @has_returned_empty_string
|
|
|
|
@has_returned_empty_string = true
|
|
|
|
''
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
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.
|