require 'mscorlib' require 'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' module Zlib MAX_WBITS = 15 Z_DEFLATED = 8 class GZipFile def close end end class GZipReader < GZipFile def initialize io @io = io ms = System::IO::MemoryStream.new @io.read.each_byte {|b| ms.WriteByte b} ms.Position = 0 @gzipstream = System::IO::Compression::GZipStream.new ms, System::IO::Compression::CompressionMode.Decompress end def read byte = 0 sb = System::Text::StringBuilder.new while true byte = @gzipstream.ReadByte break if byte == -1 sb.Append System::Convert.ToChar byte end sb.ToString end def close @io.close end end class ZStream def finish end end class Inflate < ZStream def initialize window_bits=MAX_WBITS @w_bits = window_bits if @w_bits < 0 then @rawdeflate = true @w_bits *= -1 end end def inflate zstring byte_array = [] zstring.each_byte {|b| byte_array << b} unless @rawdeflate then byte_array.reverse! compression_method_and_flags = byte_array.pop flags = byte_array.pop #CMF and FLG, when viewed as a 16-bit unsigned integer stored inMSB order (CMF*256 + FLG), is a multiple of 31 if ((compression_method_and_flags << 0x08) + flags) % 31 != 0 then raise Zlib::DataError.new("incorrect header check") end compression_method = compression_method_and_flags & 0x0F #CM = 8 denotes the “deflate” compression method with a window size up to 32K. if compression_method != Zlib::Z_DEFLATED then raise Zlib::DataError.new("unknown compression method") end compression_info = compression_method_and_flags >> 0x04 #For CM = 8, CINFO is the base-2 logarithm of the LZ77 window size,minus eight (CINFO=7 indicates a 32K window size) if (compression_info + 8) > @w_bits then raise Zlib::DataError.new("invalid window size") end preset_dictionary_flag = (flags & 0x20) >> 0x05 compression_level = (flags & 0xC0) >> 0x06 byte_array.reverse! end ms = System::IO::MemoryStream.new byte_array.each{|b| ms.WriteByte b} ms.Position = 0 deflatestream = System::IO::Compression::DeflateStream.new ms, System::IO::Compression::CompressionMode.Decompress byte = 0 sb = System::Text::StringBuilder.new while true byte = deflatestream.ReadByte break if byte == -1 sb.Append System::Convert.ToChar byte end sb.ToString end def finish end end class Error < Exception end class NeedDict < Error end class DataError < Error end end