class Zip::ExtraField

Constants

ID_MAP

Public Class Methods

new(binstr = nil) click to toggle source
# File lib/zip/extra_field.rb, line 5
def initialize(binstr = nil)
  merge(binstr) if binstr
end

Public Instance Methods

c_dir_size() click to toggle source
# File lib/zip/extra_field.rb, line 78
def c_dir_size
  to_c_dir_bin.bytesize
end
create(name) click to toggle source
# 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_item() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
length()
Alias for: local_size
local_size() click to toggle source
# File lib/zip/extra_field.rb, line 82
def local_size
  to_local_bin.bytesize
end
Also aliased as: length, size
merge(binstr) click to toggle source
# 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_values() click to toggle source

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

# File lib/zip/extra_field.rb, line 62
def ordered_values
  result = []
  each { |k, v| k == 'Unknown' ? result.push(v) : result.unshift(v) }
  result
end
size()
Alias for: local_size
to_c_dir_bin() click to toggle source
# 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_bin() click to toggle source
# 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
Also aliased as: to_s
to_s()
Alias for: to_local_bin