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.



259
260
261
# File 'lib/obf/utils.rb', line 259

def initialize(zipfile)
  @zipfile = zipfile
end

Instance Method Details

#add(path, contents) ⇒ Object



263
264
265
# File 'lib/obf/utils.rb', line 263

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

#all_filesObject



276
277
278
# File 'lib/obf/utils.rb', line 276

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

#glob(path) ⇒ Object



272
273
274
# File 'lib/obf/utils.rb', line 272

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

#read(path) ⇒ Object



267
268
269
270
# File 'lib/obf/utils.rb', line 267

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

#read_as_data(path) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/obf/utils.rb', line 280

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/)
    fn = OBF::Utils.temp_path('file')
    file = Tempfile.new('file')
    file.binmode
    file.write raw
    file.close
    data = `identify -verbose #{file.path}`
    data.split(/\n/).each do |line|
      pre, post = line.sub(/^\s+/, '').split(/:\s/, 2)
      if pre == 'Geometry'
        match = post.match(/(\d+)x(\d+)/)
        if match && match[1] && match[2]
          attrs['width'] = match[1].to_i
          attrs['height'] = match[2].to_i
        end
      end
    end
  end
  attrs
end