rubyzip/lib/zip/inflater.rb

53 lines
1.3 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)
end
2011-11-18 04:53:04 +08:00
2020-01-05 23:06:05 +08:00
def read(length = nil, outbuf = '')
return ((length.nil? || length.zero?) ? "" : nil) if eof
2019-12-24 03:57:47 +08:00
while length.nil? || (@buffer.bytesize < length)
break if input_finished?
@buffer << produce_input
end
2019-12-24 03:57:47 +08:00
outbuf.replace(@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
def produce_input
retried = 0
begin
@zlib_inflater.inflate(input_stream.read(Decompressor::CHUNK_SIZE))
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
2020-02-01 21:19:15 +08:00
rescue Zlib::Error
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
end
2019-12-21 03:01:53 +08:00
::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.