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

Version Path
spatial_features-2.17.2 lib/spatial_features/unzip.rb
spatial_features-2.17.1 lib/spatial_features/unzip.rb
spatial_features-2.17.0 lib/spatial_features/unzip.rb
spatial_features-2.16.0 lib/spatial_features/unzip.rb
spatial_features-2.15.1 lib/spatial_features/unzip.rb
spatial_features-2.15.0 lib/spatial_features/unzip.rb
spatial_features-2.14.1 lib/spatial_features/unzip.rb
spatial_features-2.14.0 lib/spatial_features/unzip.rb
spatial_features-2.13.0 lib/spatial_features/unzip.rb
spatial_features-2.12.3 lib/spatial_features/unzip.rb
spatial_features-2.12.2 lib/spatial_features/unzip.rb
spatial_features-2.12.1 lib/spatial_features/unzip.rb
spatial_features-2.12.0 lib/spatial_features/unzip.rb
spatial_features-2.11.1 lib/spatial_features/unzip.rb
spatial_features-2.11.0 lib/spatial_features/unzip.rb
spatial_features-2.10.9 lib/spatial_features/unzip.rb
spatial_features-2.10.8 lib/spatial_features/unzip.rb
spatial_features-2.10.7 lib/spatial_features/unzip.rb
spatial_features-2.10.6 lib/spatial_features/unzip.rb
spatial_features-2.10.5 lib/spatial_features/unzip.rb