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

Inherits:
Object
  • Object
show all
Defined in:
lib/archive/zip/codec/deflate.rb

Overview

Archive::Zip::Codec::Deflate is a handle for the deflate-inflate codec as defined in Zlib which provides convenient interfaces for writing and reading deflated streams.

Defined Under Namespace

Classes: Compress, Decompress

Constant Summary collapse

ID =

The numeric identifier assigned to this compression codec by the ZIP specification.

8
NORMAL =

A bit mask used to denote that Zlib's default compression level should be used.

0b000
MAXIMUM =

A bit mask used to denote that Zlib's highest/slowest compression level should be used.

0b010
FAST =

A bit mask used to denote that Zlib's lowest/fastest compression level should be used.

0b100
SUPER_FAST =

A bit mask used to denote that Zlib should not compress data at all.

0b110

Instance Method Summary collapse

Constructor Details

#initialize(general_purpose_flags = NORMAL) ⇒ Deflate

This method signature is part of the interface contract expected by Archive::Zip::Entry for compression codec objects.

Creates a new instance of this class using bits 1 and 2 of general_purpose_flags to select a compression level to be used by #compressor to set up a compression IO object. The constants NORMAL, MAXIMUM, FAST, and SUPER_FAST can be used for general_purpose_flags to manually set the compression level.


190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/archive/zip/codec/deflate.rb', line 190

def initialize(general_purpose_flags = NORMAL)
  @compression_level = general_purpose_flags & 0b110
  @zlib_compression_level = case @compression_level
                            when NORMAL
                              Zlib::DEFAULT_COMPRESSION
                            when MAXIMUM
                              Zlib::BEST_COMPRESSION
                            when FAST
                              Zlib::BEST_SPEED
                            when SUPER_FAST
                              Zlib::NO_COMPRESSION
                            else
                              raise Error, 'Invalid compression level'
                            end
end

Instance Method Details

#compression_methodObject

This method signature is part of the interface contract expected by Archive::Zip::Entry for compression codec objects.

Returns an integer used to flag that this compression codec is used for a particular ZIP archive entry.


241
242
243
# File 'lib/archive/zip/codec/deflate.rb', line 241

def compression_method
  ID
end

#compressor(io, &b) ⇒ Object

This method signature is part of the interface contract expected by Archive::Zip::Entry for compression codec objects.

A convenience method for creating an Archive::Zip::Codec::Deflate::Compress object using that class' open method. The compression level for the open method is pulled from the value of the general_purpose_flags argument of new.


213
214
215
# File 'lib/archive/zip/codec/deflate.rb', line 213

def compressor(io, &b)
  Compress.open(io, @zlib_compression_level, &b)
end

#decompressor(io, &b) ⇒ Object

This method signature is part of the interface contract expected by Archive::Zip::Entry for compression codec objects.

A convenience method for creating an Archive::Zip::Codec::Deflate::Decompress object using that class' open method.


223
224
225
# File 'lib/archive/zip/codec/deflate.rb', line 223

def decompressor(io, &b)
  Decompress.open(io, &b)
end

#general_purpose_flagsObject

This method signature is part of the interface contract expected by Archive::Zip::Entry for compression codec objects.

Returns an integer representing the general purpose flags of a ZIP archive entry where bits 1 and 2 are set according to the compression level selected for this object. All other bits are zero'd out.


251
252
253
# File 'lib/archive/zip/codec/deflate.rb', line 251

def general_purpose_flags
  @compression_level
end

#version_needed_to_extractObject

This method signature is part of the interface contract expected by Archive::Zip::Entry for compression codec objects.

Returns an integer which indicates the version of the official ZIP specification which introduced support for this compression codec.


232
233
234
# File 'lib/archive/zip/codec/deflate.rb', line 232

def version_needed_to_extract
  0x0014
end