Class: Zip::ExtraField

Inherits:
Hash
  • Object
show all
Defined in:
lib/zip/extra_field.rb

Overview

:nodoc:all

Defined Under Namespace

Classes: AES, Generic, IUnix, NTFS, OldUnix, UniversalTime, Unknown, Zip64

Constant Summary collapse

ID_MAP =
{}

Instance Method Summary collapse

Constructor Details

#initialize(binstr = nil, local: false) ⇒ ExtraField

Returns a new instance of ExtraField.



7
8
9
# File 'lib/zip/extra_field.rb', line 7

def initialize(binstr = nil, local: false)
  merge(binstr, local: local) if binstr
end

Instance Method Details

#c_dir_sizeObject



81
82
83
# File 'lib/zip/extra_field.rb', line 81

def c_dir_size
  to_c_dir_bin.bytesize
end

#create(name) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/zip/extra_field.rb', line 48

def create(name)
  unless (field_class = ID_MAP.values.find { |k| k.name == name })
    raise Error, "Unknown extra field '#{name}'"
  end

  self[name] = field_class.new
end

#extra_field_type_exist(binstr, id, len, index) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/zip/extra_field.rb', line 11

def extra_field_type_exist(binstr, id, len, index)
  field_name = ID_MAP[id].name
  if member?(field_name)
    self[field_name].merge(binstr[index, len + 4])
  else
    field_obj        = ID_MAP[id].new(binstr[index, len + 4])
    self[field_name] = field_obj
  end
end

#extra_field_type_unknown(binstr, len, index, local) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/zip/extra_field.rb', line 21

def extra_field_type_unknown(binstr, len, index, local)
  self[:unknown] ||= Unknown.new

  if !len || len + 4 > binstr[index..].bytesize
    self[:unknown].merge(binstr[index..], local: local)
    return
  end

  self[:unknown].merge(binstr[index, len + 4], local: local)
end

#local_sizeObject Also known as: length, size



85
86
87
# File 'lib/zip/extra_field.rb', line 85

def local_size
  to_local_bin.bytesize
end

#merge(binstr, local: false) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/zip/extra_field.rb', line 32

def merge(binstr, local: false)
  return if binstr.empty?

  i = 0
  while i < binstr.bytesize
    id  = binstr[i, 2]
    len = binstr[i + 2, 2].to_s.unpack1('v')
    if id && ID_MAP.member?(id)
      extra_field_type_exist(binstr, id, len, i)
    elsif id
      break unless extra_field_type_unknown(binstr, len, i, local)
    end
    i += len + 4
  end
end

#ordered_valuesObject

Place Unknown last, so “extra” data that is missing the proper signature/size does not prevent known fields from being read back in.



58
59
60
61
62
# File 'lib/zip/extra_field.rb', line 58

def ordered_values
  result = []
  each { |k, v| k == :unknown ? result.push(v) : result.unshift(v) }
  result
end

#suppress_fields!(fields) ⇒ Object

Remove any extra fields that indicate they can be safely suppressed.



65
66
67
68
69
# File 'lib/zip/extra_field.rb', line 65

def suppress_fields!(fields)
  reject! do |k, v|
    v.suppress? if fields == true || [*fields].include?(k)
  end
end

#to_c_dir_binObject



77
78
79
# File 'lib/zip/extra_field.rb', line 77

def to_c_dir_bin
  ordered_values.map! { |v| v.to_c_dir_bin.force_encoding('BINARY') }.join
end

#to_local_binObject Also known as: to_s



71
72
73
# File 'lib/zip/extra_field.rb', line 71

def to_local_bin
  ordered_values.map! { |v| v.to_local_bin.force_encoding('BINARY') }.join
end