Class: NdrImport::File::Zip

Inherits:
Base
  • Object
show all
Defined in:
lib/ndr_import/file/zip.rb

Overview

This class is a zip file handler that returns tables from the extracted files.

Instance Method Summary collapse

Constructor Details

#initialize(filename, format, options = {}) ⇒ Zip

Returns a new instance of Zip.



11
12
13
14
15
16
17
# File 'lib/ndr_import/file/zip.rb', line 11

def initialize(filename, format, options = {})
  super
  @pattern = options['pattern'] || //
  @unzip_path = options['unzip_path']

  validate_unzip_path_is_safe!
end

Instance Method Details

#files(&block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ndr_import/file/zip.rb', line 19

def files(&block)
  fail 'Not allowed in external environment' if defined?(::Rails) && ::Rails.env.external?

  return enum_for(:files) unless block

  destination = @unzip_path.join(Time.current.strftime('%H%M%S%L'))
  FileUtils.mkdir_p(SafeFile.safepath_to_string(destination))

  ::Zip::File.open(SafeFile.safepath_to_string(@filename)) do |zipfile|
    unzip_entries(zipfile, destination, &block)
  end
end

#tablesObject

Zip files produce files, never tables.



33
34
35
# File 'lib/ndr_import/file/zip.rb', line 33

def tables
  fail 'Zip#tables should never be called'
end