Class: Zip::ExtraField

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

Defined Under Namespace

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

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


74
75
76
# File 'lib/zip/extra_field.rb', line 74

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..-1].bytesize
    self['Unknown'].merge(binstr[index..-1], local: local)
    return
  end

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

#local_sizeObject Also known as: length, size


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

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

#to_c_dir_binObject


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

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


64
65
66
# File 'lib/zip/extra_field.rb', line 64

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