Sha256: 6cbf6015f8c321311d18497b7ba3006751fa9fab1627c45c89d4c94b7e0da098

Contents?: true

Size: 1.01 KB

Versions: 15

Compression:

Stored size: 1.01 KB

Contents

module Fontist
  module Utils
    module SevenZipExtractor
      def seven_zip_extract(resource, extension: /\.cab$/)
        file = download_file(resource)

        extract_seven_zip_file(file, extension)
      end

      private

      def extract_seven_zip_file(file, extension)
        File.open(file, "rb") do |zip_file|
          reader.open(zip_file) do |szr|
            path = extract_by_extension(szr, extension)

            return block_given? ? yield(path) : path
          end
        end
      end

      def reader
        @reader ||= begin
                      require "seven_zip_ruby"
                      SevenZipRuby::Reader
                    end
      end

      def extract_by_extension(szr, extension)
        cab_entry = szr.entries.detect do |entry|
          entry.file? && entry.path.match(extension)
        end

        tmp_dir = Dir.mktmpdir
        szr.extract(cab_entry, tmp_dir)
        filename = Pathname.new(cab_entry.path).basename
        File.join(tmp_dir, filename)
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
fontist-1.8.7 lib/fontist/utils/seven_zip_extractor.rb
fontist-1.8.6 lib/fontist/utils/seven_zip_extractor.rb
fontist-1.8.5 lib/fontist/utils/seven_zip_extractor.rb
fontist-1.8.4 lib/fontist/utils/seven_zip_extractor.rb
fontist-1.8.3 lib/fontist/utils/seven_zip_extractor.rb
fontist-1.8.2 lib/fontist/utils/seven_zip_extractor.rb
fontist-1.8.1 lib/fontist/utils/seven_zip_extractor.rb
fontist-1.7.3 lib/fontist/utils/seven_zip_extractor.rb
fontist-1.7.2 lib/fontist/utils/seven_zip_extractor.rb
fontist-1.7.1 lib/fontist/utils/seven_zip_extractor.rb
fontist-1.7.0 lib/fontist/utils/seven_zip_extractor.rb
fontist-1.6.0 lib/fontist/utils/seven_zip_extractor.rb
fontist-1.5.1 lib/fontist/utils/seven_zip_extractor.rb
fontist-1.5.0 lib/fontist/utils/seven_zip_extractor.rb
fontist-1.4.0 lib/fontist/utils/seven_zip_extractor.rb