Class: OBF::Utils::Zipper

Inherits:
Object
  • Object
show all
Defined in:
lib/obf/utils.rb

Instance Method Summary collapse

Constructor Details

#initialize(zipfile) ⇒ Zipper

Returns a new instance of Zipper.



420
421
422
# File 'lib/obf/utils.rb', line 420

def initialize(zipfile)
  @zipfile = zipfile
end

Instance Method Details

#add(path, contents) ⇒ Object



424
425
426
# File 'lib/obf/utils.rb', line 424

def add(path, contents)
  @zipfile.get_output_stream(path) {|os| os.write contents }
end

#all_filesObject



437
438
439
# File 'lib/obf/utils.rb', line 437

def all_files
  @zipfile.entries.select{|e| e.file? }.map{|e| e.to_s }
end

#glob(path) ⇒ Object



433
434
435
# File 'lib/obf/utils.rb', line 433

def glob(path)
  @zipfile.glob(path)
end

#read(path) ⇒ Object



428
429
430
431
# File 'lib/obf/utils.rb', line 428

def read(path)
  entry = @zipfile.glob(path).first rescue nil
  entry ? entry.get_input_stream.read : nil
end

#read_as_data(path) ⇒ Object



441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/obf/utils.rb', line 441

def read_as_data(path)
  attrs = {}
  raw = @zipfile.read(path)
  types = MIME::Types.type_for(path)
  attrs['content_type'] = types[0] && types[0].to_s

  str = "data:" + attrs['content_type']
  str += ";base64," + Base64.strict_encode64(raw)
  attrs['data'] = str

  if attrs['content_type'].match(/^image/)
    file = Tempfile.new('file')
    file.binmode
    file.write raw
    file.close
    more_attrs = OBF::Utils.image_attrs(file.path)
    attrs['content_type'] ||= more_attrs['content_type']
    attrs['width'] ||= more_attrs['width']
    attrs['height'] ||= more_attrs['height']
  end
  attrs
end