Sha256: 4f1354c52d120c44c61e9ddcc03028d400bf6a9a7d90e664b2618f136a7a5128

Contents?: true

Size: 993 Bytes

Versions: 8

Compression:

Stored size: 993 Bytes

Contents

require "zip"
require "pathname"

module Fontist
  module Utils
    module ZipExtractor
      def zip_extract(resource, download: true, fonts_sub_dir: "")
        zip_file = download_file(resource) if download
        zip_file ||= resource.urls.first

        fonts_paths = unzip_fonts(zip_file, fonts_sub_dir)
        block_given? ? yield(fonts_paths) : fonts_paths
      end

      alias_method :unzip, :zip_extract

      private

      def unzip_fonts(file, fonts_sub_dir = "")
        Zip.on_exists_proc = true
        Array.new.tap do |fonts|

          Zip::File.open(file) do |zip_file|
            zip_file.glob("#{fonts_sub_dir}*.{ttf,ttc,otf}").each do |entry|
              if entry.name
                filename = Pathname.new(entry.name).basename
                font_path = fonts_path.join(filename.to_s)
                fonts.push(font_path.to_s)

                entry.extract(font_path)
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 7 versions & 2 rubygems

Version Path
fontist-1.3.0 lib/fontist/utils/zip_extractor.rb
fontist-1.2.1 lib/fontist/utils/zip_extractor.rb
metanorma-cli-1.3.4 gems/ruby/2.6.0/gems/fontist-1.1.0/lib/fontist/utils/zip_extractor.rb
metanorma-cli-1.3.4 gems/ruby/2.6.0/gems/fontist-1.2.0/lib/fontist/utils/zip_extractor.rb
fontist-1.2.0 lib/fontist/utils/zip_extractor.rb
metanorma-cli-1.3.3.1 gems/ruby/2.6.0/gems/fontist-1.1.0/lib/fontist/utils/zip_extractor.rb
fontist-1.1.0 lib/fontist/utils/zip_extractor.rb
fontist-1.0.0 lib/fontist/utils/zip_extractor.rb