Class: Zip::ExtraField

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

Defined Under Namespace

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

Constant Summary collapse

ID_MAP =
{}

Instance Method Summary collapse

Constructor Details

#initialize(binstr = nil) ⇒ ExtraField

Returns a new instance of ExtraField.



5
6
7
# File 'lib/zip/extra_field.rb', line 5

def initialize(binstr = nil)
  binstr and merge(binstr)
end

Instance Method Details

#c_dir_sizeObject



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

def c_dir_size
  to_c_dir_bin.bytesize
end

#create(name) ⇒ Object



53
54
55
56
57
58
# File 'lib/zip/extra_field.rb', line 53

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

#create_unknown_itemObject



28
29
30
31
32
33
34
35
# File 'lib/zip/extra_field.rb', line 28

def create_unknown_item
  s = ''
  class << s
    alias_method :to_c_dir_bin, :to_s
    alias_method :to_local_bin, :to_s
  end
  self['Unknown'] = s
end

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



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

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

#extra_field_type_unknown(binstr, len, i) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/zip/extra_field.rb', line 19

def extra_field_type_unknown(binstr, len, i)
  create_unknown_item unless self['Unknown']
  if !len || len + 4 > binstr[i..-1].bytesize
    self['Unknown'] << binstr[i..-1]
    return
  end
  self['Unknown'] << binstr[i, len + 4]
end

#local_sizeObject Also known as: length, size



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

def local_size
  to_local_bin.bytesize
end

#merge(binstr) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/zip/extra_field.rb', line 37

def merge(binstr)
  return if binstr.empty?
  i = 0
  while i < binstr.bytesize
    id  = binstr[i, 2]
    len = binstr[i + 2, 2].to_s.unpack('v').first
    if id && ID_MAP.member?(id)
      extra_field_type_exist(binstr, id, len, i)
    elsif id
      create_unknown_item unless self['Unknown']
      break unless extra_field_type_unknown(binstr, len, i)
    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



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

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

#to_c_dir_binObject



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

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



68
69
70
# File 'lib/zip/extra_field.rb', line 68

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