rubyzip/lib/zip/inflater.rb

58 lines
1.4 KiB
Ruby
Raw Normal View History

module Zip
2013-06-03 02:33:03 +08:00
class Inflater < Decompressor #:nodoc:all
2019-12-24 03:57:47 +08:00
def initialize(*args)
super
@buffer = ''.dup
@zlib_inflater = ::Zlib::Inflate.new(-Zlib::MAX_WBITS)
2013-06-03 02:33:03 +08:00
@has_returned_empty_string = false
end
2011-11-18 04:53:04 +08:00
2019-12-24 03:57:47 +08:00
def sysread(length = nil, buf = '')
while length.nil? || (@buffer.bytesize < length)
break if input_finished?
@buffer << produce_input(buf)
end
2019-12-24 03:57:47 +08:00
return value_when_finished if eof?
@buffer.slice!(0...(length || @buffer.bytesize))
end
2011-11-18 04:53:04 +08:00
def eof
2019-12-24 03:57:47 +08:00
@buffer.empty? && input_finished?
end
2013-06-03 02:33:03 +08:00
alias_method :eof?, :eof
private
2019-12-24 03:57:47 +08:00
def produce_input(buf = '')
retried = 0
begin
2020-01-08 17:58:25 +08:00
@zlib_inflater.inflate(input_stream.read(Decompressor::CHUNK_SIZE, buf))
rescue Zlib::BufError
2013-06-03 02:33:03 +08:00
raise if retried >= 5 # how many times should we retry?
retried += 1
retry
end
rescue Zlib::Error => e
raise(::Zip::DecompressionError, 'zlib error while inflating')
end
2019-12-24 03:57:47 +08:00
def input_finished?
2013-06-03 02:33:03 +08:00
@zlib_inflater.finished?
end
def value_when_finished # mimic behaviour of ruby File object.
return if @has_returned_empty_string
@has_returned_empty_string = true
''
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.