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.



316
317
318
# File 'lib/obf/utils.rb', line 316

def initialize(zipfile)
  @zipfile = zipfile
end

Instance Method Details

#add(path, contents) ⇒ Object



320
321
322
# File 'lib/obf/utils.rb', line 320

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

#all_filesObject



333
334
335
# File 'lib/obf/utils.rb', line 333

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

#glob(path) ⇒ Object



329
330
331
# File 'lib/obf/utils.rb', line 329

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

#read(path) ⇒ Object



324
325
326
327
# File 'lib/obf/utils.rb', line 324

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

#read_as_data(path) ⇒ Object



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/obf/utils.rb', line 337

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