Class: Archive::Zip::Codec::Deflate::Compress
- Inherits:
-
Zlib::ZWriter
- Object
- Zlib::ZWriter
- Archive::Zip::Codec::Deflate::Compress
- 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
-
#crc32 ⇒ Object
(also: #checksum)
readonly
The CRC32 checksum of the uncompressed data written using this object.
Attributes inherited from Zlib::ZWriter
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
-
#close(close_delegate = true) ⇒ Object
Closes this object so that further write operations will fail.
-
#data_descriptor ⇒ Object
Returns an instance of Archive::Zip::DataDescriptor with information regarding the data which has passed through this object to the delegate object.
-
#initialize(io, compression_level) ⇒ Compress
constructor
Creates a new instance of this class using io as a data sink.
- #unbuffered_seek(offset, whence = IO::SEEK_SET) ⇒ Object private
- #unbuffered_write(string) ⇒ Object private
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
#crc32 ⇒ Object (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_descriptor ⇒ Object
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 |
#unbuffered_seek(offset, whence = IO::SEEK_SET) ⇒ Object (private)
76 77 78 79 80 |
# File 'lib/archive/zip/codec/deflate.rb', line 76 def unbuffered_seek(offset, whence = IO::SEEK_SET) result = super(offset, whence) @crc32 = 0 if whence == IO::SEEK_SET result end |
#unbuffered_write(string) ⇒ Object (private)
82 83 84 85 86 |
# File 'lib/archive/zip/codec/deflate.rb', line 82 def unbuffered_write(string) result = super(string) @crc32 = Zlib.crc32(string, @crc32) result end |