rubyzip/lib/zip/deflater.rb

32 lines
956 B
Ruby
Raw Normal View History

module Zip
class Deflater < Compressor #:nodoc:all
2013-06-03 02:33:03 +08:00
2014-12-31 00:03:17 +08:00
def initialize(output_stream, level = Zip.default_compression, encrypter = NullEncrypter.new)
super()
2013-06-03 02:33:03 +08:00
@output_stream = output_stream
@zlib_deflater = ::Zlib::Deflate.new(level, -::Zlib::MAX_WBITS)
@size = 0
@crc = ::Zlib.crc32
2014-12-31 00:03:17 +08:00
@encrypter = encrypter
@output_stream << @encrypter.header(@crc)
end
2013-06-03 02:33:03 +08:00
def << (data)
2013-06-03 02:33:03 +08:00
val = data.to_s
@crc = Zlib::crc32(val, @crc)
2014-12-31 00:03:17 +08:00
@size += val.bytesize + @encrypter.header_bytesize
@output_stream << @encrypter.encrypt(@zlib_deflater.deflate(data))
end
def finish
2014-12-31 00:03:17 +08:00
@output_stream << @encrypter.encrypt(@zlib_deflater.finish) until @zlib_deflater.finished?
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.