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.



360
361
362
# File 'lib/obf/utils.rb', line 360

def initialize(zipfile)
  @zipfile = zipfile
end

Instance Method Details

#add(path, contents) ⇒ Object



364
365
366
# File 'lib/obf/utils.rb', line 364

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

#all_filesObject



377
378
379
# File 'lib/obf/utils.rb', line 377

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

#glob(path) ⇒ Object



373
374
375
# File 'lib/obf/utils.rb', line 373

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

#read(path) ⇒ Object



368
369
370
371
# File 'lib/obf/utils.rb', line 368

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

#read_as_data(path) ⇒ Object



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/obf/utils.rb', line 381

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