Sha256: 317edc9be80c1a663999545739be66b022cd08813db8b088b2a52d449bbc6287
Contents?: true
Size: 1.3 KB
Versions: 20
Compression:
Stored size: 1.3 KB
Contents
require 'fileutils' module SpatialFeatures module Unzip def self.paths(file_path, find: nil, **extract_options) paths = extract(file_path, **extract_options) 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 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 def self.names(file_path) entries(file_path).collect(&:name) end def self.entries(file_path) Zip::File.open(File.path(file_path)) end def self.is_zip?(file) zip = file.readline.start_with?('PK') file.rewind return zip rescue EOFError return false end # EXCEPTIONS class PathNotFound < StandardError; end end end
Version data entries
20 entries across 20 versions & 1 rubygems