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

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

Overview

reading deflated streams.

Defined Under Namespace

Classes: Reader, Writer

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.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/archive/zip/codec/deflate.rb', line 39

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.



90
91
92
# File 'lib/archive/zip/codec/deflate.rb', line 90

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::Writer 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.



62
63
64
# File 'lib/archive/zip/codec/deflate.rb', line 62

def compressor(io, &b)
  Writer.open(io, level: @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::Reader object using that class’ open method.



72
73
74
# File 'lib/archive/zip/codec/deflate.rb', line 72

def decompressor(io, &b)
  Reader.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.



100
101
102
# File 'lib/archive/zip/codec/deflate.rb', line 100

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.



81
82
83
# File 'lib/archive/zip/codec/deflate.rb', line 81

def version_needed_to_extract
  0x0014
end