2010-11-30 16:27:59 +08:00
|
|
|
module Zip
|
|
|
|
class Deflater < Compressor #:nodoc:all
|
2014-12-31 00:03:17 +08:00
|
|
|
def initialize(output_stream, level = Zip.default_compression, encrypter = NullEncrypter.new)
|
2010-11-30 16:27:59 +08:00
|
|
|
super()
|
2013-06-03 02:33:03 +08:00
|
|
|
@output_stream = output_stream
|
|
|
|
@zlib_deflater = ::Zlib::Deflate.new(level, -::Zlib::MAX_WBITS)
|
2015-01-07 09:45:01 +08:00
|
|
|
@size = 0
|
2013-08-15 06:00:27 +08:00
|
|
|
@crc = ::Zlib.crc32
|
2014-12-31 00:03:17 +08:00
|
|
|
@encrypter = encrypter
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
2013-06-03 02:33:03 +08:00
|
|
|
|
2015-03-23 00:43:44 +08:00
|
|
|
def <<(data)
|
2013-06-03 02:33:03 +08:00
|
|
|
val = data.to_s
|
2015-03-21 16:16:06 +08:00
|
|
|
@crc = Zlib.crc32(val, @crc)
|
2015-01-04 21:49:58 +08:00
|
|
|
@size += val.bytesize
|
2020-04-29 21:38:33 +08:00
|
|
|
buffer = @zlib_deflater.deflate(data, Zlib::SYNC_FLUSH)
|
2020-04-29 22:06:29 +08:00
|
|
|
return @output_stream if buffer.empty?
|
|
|
|
|
|
|
|
@output_stream << @encrypter.encrypt(buffer)
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def finish
|
2020-04-29 21:38:33 +08:00
|
|
|
buffer = @zlib_deflater.finish
|
|
|
|
@output_stream << @encrypter.encrypt(buffer) unless buffer.empty?
|
|
|
|
@zlib_deflater.close
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :size, :crc
|
|
|
|
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.
|