Module: Archive::Zip::Codec

Defined in:
lib/archive/zip/codec.rb,
lib/archive/zip/codec/store.rb,
lib/archive/zip/codec/deflate.rb,
lib/archive/zip/codec/null_encryption.rb,
lib/archive/zip/codec/traditional_encryption.rb

Overview

Archive::Zip::Codec is a factory class for generating codec object instances based on the compression method and general purpose flag fields of ZIP entries. When adding a new codec, add a mapping in the CODECS constant from the compression method field value reserved for the codec in the ZIP specification to the class implementing the codec. See the implementations of Archive::Zip::Codec::Deflate and Archive::Zip::Codec::Store for details on implementing custom codecs.

Defined Under Namespace

Classes: Deflate, NullEncryption, Store, TraditionalEncryption

Constant Summary collapse

COMPRESSION_CODECS =

A Hash mapping compression methods to compression codec implementations. New compression codecs must add a mapping here when defined in order to be used.

{}
ENCRYPTION_CODECS =

A Hash mapping encryption methods to encryption codec implementations. New encryption codecs must add a mapping here when defined in order to be used.

{}

Class Method Summary collapse

Class Method Details

.create_compression_codec(compression_method, general_purpose_flags) ⇒ Object

Returns a new compression codec instance based on compression_method and general_purpose_flags.

Raises:



24
25
26
27
28
29
30
31
32
# File 'lib/archive/zip/codec.rb', line 24

def self.create_compression_codec(compression_method, general_purpose_flags)
  # Load the standard compression codecs.
  require 'archive/zip/codec/deflate'
  require 'archive/zip/codec/store'

  codec = COMPRESSION_CODECS[compression_method].new(general_purpose_flags)
  raise Zip::Error, 'unsupported compression codec' if codec.nil?
  codec
end

.create_encryption_codec(general_purpose_flags) ⇒ Object

Returns a new encryption codec instance based on general_purpose_flags.

NOTE: The signature of this method will have to change in order to support the strong encryption codecs. This is intended to be an internal method anyway, so this fact should not cause major issues for users of this library.

Raises:



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

def self.create_encryption_codec(general_purpose_flags)
  general_purpose_flags &= 0b0000000001000001
  if general_purpose_flags == 0b0000000000000000 then
    require 'archive/zip/codec/null_encryption'
    codec = NullEncryption.new
  elsif general_purpose_flags == 0b0000000000000001 then
    require 'archive/zip/codec/traditional_encryption'
    codec = TraditionalEncryption.new
  end
  raise Zip::Error, 'unsupported encryption codec' if codec.nil?
  codec
end