Class: Archive::Zip::Codec::Deflate::Compress

Inherits:
Zlib::ZWriter show all
Defined in:
lib/archive/zip/codec/deflate.rb

Overview

Archive::Zip::Codec::Deflate::Compress extends Zlib::ZWriter in order to specify the standard Zlib options required by ZIP archives and to provide a close method which can optionally close the delegate IO-like object. In addition a convenience method is provided for generating DataDescriptor objects based on the data which is passed through this object.

Instances of this class should only be accessed via the Archive::Zip::Codec::Deflate#compressor method.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Zlib::ZWriter

#compressed_size, #uncompressed_size

Constructor Details

#initialize(io, compression_level) ⇒ Compress

Creates a new instance of this class using io as a data sink. io must be writable and must provide a write method as IO does or errors will be raised when performing write operations. compression_level must be one of Zlib::DEFAULT_COMPRESSION, Zlib::BEST_COMPRESSION, Zlib::BEST_SPEED, or Zlib::NO_COMPRESSION and specifies the amount of compression to be applied to the data stream.



43
44
45
46
# File 'lib/archive/zip/codec/deflate.rb', line 43

def initialize(io, compression_level)
  super(io, compression_level, -Zlib::MAX_WBITS)
  @crc32 = 0
end

Instance Attribute Details

#crc32Object (readonly) Also known as: checksum

The CRC32 checksum of the uncompressed data written using this object.

NOTE: Anything still in the internal write buffer has not been processed, so calling #flush prior to examining this attribute may be necessary for an accurate computation.



53
54
55
# File 'lib/archive/zip/codec/deflate.rb', line 53

def crc32
  @crc32
end

Class Method Details

.open(io, compression_level) ⇒ Object

Creates a new instance of this class with the given arguments using #new and then passes the instance to the given block. The #close method is guaranteed to be called after the block completes.

Equivalent to #new if no block is given.



26
27
28
29
30
31
32
33
34
35
# File 'lib/archive/zip/codec/deflate.rb', line 26

def self.open(io, compression_level)
  deflate_io = new(io, compression_level)
  return deflate_io unless block_given?

  begin
    yield(deflate_io)
  ensure
    deflate_io.close unless deflate_io.closed?
  end
end

Instance Method Details

#close(close_delegate = true) ⇒ Object

Closes this object so that further write operations will fail. If close_delegate is true, the delegate object used as a data sink will also be closed using its close method.



59
60
61
62
# File 'lib/archive/zip/codec/deflate.rb', line 59

def close(close_delegate = true)
  super()
  delegate.close if close_delegate
end

#data_descriptorObject

Returns an instance of Archive::Zip::DataDescriptor with information regarding the data which has passed through this object to the delegate object. The close or flush methods should be called before using this method in order to ensure that any possibly buffered data is flushed to the delegate object; otherwise, the contents of the data descriptor may be inaccurate.



70
71
72
# File 'lib/archive/zip/codec/deflate.rb', line 70

def data_descriptor
  DataDescriptor.new(crc32, compressed_size, uncompressed_size)
end