Class: Bestchange::ZipExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/bestchange/zip_extractor.rb

Instance Method Summary collapse

Constructor Details

#initialize(zip_file) ⇒ ZipExtractor

Returns a new instance of ZipExtractor.



7
8
9
# File 'lib/bestchange/zip_extractor.rb', line 7

def initialize(zip_file)
  @zip_file = zip_file
end

Instance Method Details

#call(filename) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bestchange/zip_extractor.rb', line 11

def call(filename)
  tempfile = Tempfile.new(filename)

  Zip::File.open(@zip_file) do |zip_file|
    entry = zip_file.find_entry(filename)

    entry.extract(tempfile) { true }
  end

  tempfile.rewind

  if block_given?
    begin
      yield tempfile
    ensure
      tempfile.close
    end
  else
    tempfile
  end
end