Sha256: 5c5aa8ae499b153fe3ea17b8dceb8c283c0fa9d182d43cd3535f4f0f0f3aaba0

Contents?: true

Size: 1.3 KB

Versions: 3

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.select {|path| find.any? {|pattern| path.index(pattern) } }
        raise(PathNotFound, "Archive did not contain a file matching #{find}") if paths.empty?
      end

      return Array(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

3 entries across 3 versions & 1 rubygems

Version Path
spatial_features-2.19.0 lib/spatial_features/unzip.rb
spatial_features-2.18.0 lib/spatial_features/unzip.rb
spatial_features-2.17.3 lib/spatial_features/unzip.rb