Module: SpatialFeatures::Unzip

Defined in:
lib/spatial_features/unzip.rb

Class Method Summary collapse

Class Method Details

.entries(file_path) ⇒ Object



34
35
36
# File 'lib/spatial_features/unzip.rb', line 34

def self.entries(file_path)
  Zip::File.open(File.path(file_path))
end

.extract(file_path, output_dir = Dir.mktmpdir) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/spatial_features/unzip.rb', line 16

def self.extract(file_path, output_dir = Dir.mktmpdir)
  [].tap do |paths|
    entries(file_path).each do |entry|
      path = "#{output_dir}/#{entry.name}"
      FileUtils.mkdir_p(File.dirname(path))
      entry.extract(path)
      paths << path
    end
  end
rescue => e
  FileUtils.remove_entry(output_dir)
  raise(e)
end

.is_zip?(file) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/spatial_features/unzip.rb', line 38

def self.is_zip?(file)
  zip = file.readline.start_with?('PK')
  file.rewind
  return zip
rescue EOFError
  return false
end

.names(file_path) ⇒ Object



30
31
32
# File 'lib/spatial_features/unzip.rb', line 30

def self.names(file_path)
  entries(file_path).collect(&:name)
end

.paths(file_path, find: nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/spatial_features/unzip.rb', line 5

def self.paths(file_path, find: nil)
  paths = extract(file_path)

  if find = Array.wrap(find).presence
    paths = paths.detect {|path| find.any? {|pattern| path.include?(pattern) } }
    raise(ImportError, "No file matched #{find}") unless paths.present?
  end

  return paths
end