Module: SpatialFeatures::Unzip
- Defined in:
- lib/spatial_features/unzip.rb
Defined Under Namespace
Classes: PathNotFound
Class Method Summary collapse
- .entries(file_path) ⇒ Object
- .extract(file_path, output_dir = Dir.mktmpdir, downcase: false) ⇒ Object
- .is_zip?(file) ⇒ Boolean
- .names(file_path) ⇒ Object
- .paths(file_path, find: nil, **extract_options) ⇒ Object
Class Method Details
.entries(file_path) ⇒ Object
36 37 38 |
# File 'lib/spatial_features/unzip.rb', line 36 def self.entries(file_path) Zip::File.open(File.path(file_path)) end |
.extract(file_path, output_dir = Dir.mktmpdir, downcase: false) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/spatial_features/unzip.rb', line 16 def self.extract(file_path, output_dir = Dir.mktmpdir, downcase: false) [].tap do |paths| entries(file_path).each do |entry| output_filename = entry.name output_filename = output_filename.downcase if downcase path = "#{output_dir}/#{output_filename}" 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
40 41 42 43 44 45 46 |
# File 'lib/spatial_features/unzip.rb', line 40 def self.is_zip?(file) zip = file.readline.start_with?('PK') file.rewind return zip rescue EOFError return false end |
.names(file_path) ⇒ Object
32 33 34 |
# File 'lib/spatial_features/unzip.rb', line 32 def self.names(file_path) entries(file_path).collect(&:name) end |
.paths(file_path, find: nil, **extract_options) ⇒ 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.index(pattern) } } raise(PathNotFound, "Archive did not contain a file matching #{find}") unless paths.present? end return paths end |