2021-05-24 01:24:22 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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-24 03:57:47 +08:00
|
|
|
def initialize(*args)
|
|
|
|
super
|
|
|
|
|
2020-02-05 10:40:56 +08:00
|
|
|
@buffer = +''
|
2019-12-24 03:57:47 +08:00
|
|
|
@zlib_inflater = ::Zlib::Inflate.new(-Zlib::MAX_WBITS)
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
2011-11-18 04:53:04 +08:00
|
|
|
|
2021-03-06 08:36:09 +08:00
|
|
|
def read(length = nil, outbuf = +'')
|
2020-02-10 06:44:08 +08:00
|
|
|
return (length.nil? || length.zero? ? '' : nil) if eof
|
2020-01-05 23:09:18 +08:00
|
|
|
|
2019-12-24 03:57:47 +08:00
|
|
|
while length.nil? || (@buffer.bytesize < length)
|
|
|
|
break if input_finished?
|
2020-02-09 21:13:21 +08:00
|
|
|
|
2020-01-05 22:24:22 +08:00
|
|
|
@buffer << produce_input
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
2019-12-24 03:57:47 +08:00
|
|
|
|
2020-01-05 22:24:22 +08:00
|
|
|
outbuf.replace(@buffer.slice!(0...(length || @buffer.bytesize)))
|
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
|
2019-12-24 03:57:47 +08:00
|
|
|
@buffer.empty? && input_finished?
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
2013-06-03 02:33:03 +08:00
|
|
|
|
2020-02-09 23:28:30 +08:00
|
|
|
alias eof? eof
|
2010-11-30 16:27:59 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-01-05 22:24:22 +08:00
|
|
|
def produce_input
|
2010-11-30 16:27:59 +08:00
|
|
|
retried = 0
|
|
|
|
begin
|
2020-01-05 22:24:22 +08:00
|
|
|
@zlib_inflater.inflate(input_stream.read(Decompressor::CHUNK_SIZE))
|
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?
|
2020-02-09 21:13:21 +08:00
|
|
|
|
2010-11-30 16:27:59 +08:00
|
|
|
retried += 1
|
|
|
|
retry
|
|
|
|
end
|
2020-02-01 21:19:15 +08:00
|
|
|
rescue Zlib::Error
|
2019-12-29 21:43:14 +08:00
|
|
|
raise(::Zip::DecompressionError, 'zlib error while inflating')
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
2019-12-24 03:57:47 +08:00
|
|
|
def input_finished?
|
2013-06-03 02:33:03 +08:00
|
|
|
@zlib_inflater.finished?
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
end
|
2019-12-21 03:01:53 +08:00
|
|
|
|
|
|
|
::Zip::Decompressor.register(::Zip::COMPRESSION_METHOD_DEFLATE, ::Zip::Inflater)
|
2010-11-30 16:27:59 +08:00
|
|
|
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.
|