2010-11-30 16:27:59 +08:00
|
|
|
module Zip
|
2013-06-03 02:33:03 +08:00
|
|
|
class Inflater < Decompressor #:nodoc:all
|
|
|
|
def initialize(input_stream)
|
2010-11-30 16:27:59 +08:00
|
|
|
super
|
2013-06-03 02:33:03 +08:00
|
|
|
@zlib_inflater = ::Zlib::Inflate.new(-Zlib::MAX_WBITS)
|
|
|
|
@output_buffer = ''
|
2014-02-05 02:56:56 +08:00
|
|
|
@output_buffer_pos = 0
|
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 = '')
|
2014-02-05 02:56:56 +08:00
|
|
|
buf ||= ''
|
|
|
|
buf.clear
|
2013-06-03 02:33:03 +08:00
|
|
|
readEverything = number_of_bytes.nil?
|
2014-02-05 02:56:56 +08:00
|
|
|
if readEverything
|
|
|
|
buf << @output_buffer[@output_buffer_pos...@output_buffer.bytesize]
|
|
|
|
|
|
|
|
move_output_buffer_pos(buf.bytesize)
|
|
|
|
else
|
|
|
|
buf << @output_buffer[@output_buffer_pos, number_of_bytes]
|
|
|
|
|
|
|
|
move_output_buffer_pos(buf.bytesize)
|
|
|
|
|
|
|
|
if buf.bytesize == number_of_bytes
|
|
|
|
return buf
|
|
|
|
end
|
|
|
|
end
|
|
|
|
while readEverything || buf.bytesize + @output_buffer.bytesize < number_of_bytes
|
2011-11-18 04:53:04 +08:00
|
|
|
break if internal_input_finished?
|
2014-02-05 02:56:56 +08:00
|
|
|
@output_buffer << internal_produce_input
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
2014-02-05 02:56:56 +08:00
|
|
|
return value_when_finished(number_of_bytes, buf) if @output_buffer.bytesize == 0 && input_finished?
|
|
|
|
end_index = (number_of_bytes.nil? ? @output_buffer.bytesize : number_of_bytes) - buf.bytesize
|
|
|
|
data = @output_buffer[0...end_index]
|
|
|
|
|
|
|
|
move_output_buffer_pos(data.bytesize)
|
|
|
|
|
|
|
|
buf << data
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
2011-11-18 04:53:04 +08:00
|
|
|
|
2010-11-30 16:27:59 +08:00
|
|
|
def produce_input
|
2014-02-05 02:56:56 +08:00
|
|
|
sysread()
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# to be used with produce_input, not read (as read may still have more data cached)
|
|
|
|
# is data cached anywhere other than @outputBuffer? the comment above may be wrong
|
|
|
|
def input_finished?
|
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
|
|
|
|
2010-11-30 16:27:59 +08:00
|
|
|
alias :eof :input_finished?
|
|
|
|
alias :eof? :input_finished?
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2014-02-05 02:56:56 +08:00
|
|
|
def move_output_buffer_pos(inc)
|
|
|
|
@output_buffer_pos += inc
|
|
|
|
if @output_buffer_pos == @output_buffer.bytesize
|
|
|
|
@output_buffer.clear
|
|
|
|
@output_buffer_pos = 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def internal_produce_input
|
|
|
|
buf = ''
|
2010-11-30 16:27:59 +08:00
|
|
|
retried = 0
|
|
|
|
begin
|
2013-06-03 02:33:03 +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
|
|
|
|
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-02-05 02:56:56 +08:00
|
|
|
def value_when_finished(number_of_bytes, buf) # mimic behaviour of ruby File object.
|
|
|
|
if number_of_bytes.nil?
|
|
|
|
buf
|
|
|
|
elsif buf.bytesize == 0
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
buf
|
|
|
|
end
|
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.
|