Sha256: 7973546c90e3e446ca5cc8195c7f20ecd2b49414c168cb7402d44e198e45402f

Contents?: true

Size: 1.85 KB

Versions: 7

Compression:

Stored size: 1.85 KB

Contents

require 'zip'
require 'zlib'
require 'archive/tar/minitar'

module RakeDependencies
  module Extractors
    class ZipExtractor
      def initialize(file_path, extract_path, options = {})
        @file_path = file_path
        @extract_path = extract_path
        @options = options
      end

      def extract
        FileUtils.mkdir_p(@extract_path)
        Zip::File.open(@file_path) do |zip_file_entries|
          zip_file_entries.each do |entry|
            entry_pathname = Pathname.new(entry.name)
            strip_pathname = Pathname.new(@options[:strip_path] || '')
            target_pathname = entry_pathname.relative_path_from(strip_pathname)

            file_path = File.join(@extract_path, target_pathname)
            FileUtils.mkdir_p(File.dirname(file_path))
            zip_file_entries.extract(entry, file_path) unless File.exist?(file_path)
          end
        end
      end
    end

    class TarGzExtractor
      def initialize(file_path, extract_path, options = {})
        @file_path = file_path
        @extract_path = extract_path
        @options = options
      end

      def extract
        FileUtils.mkdir_p(@extract_path)
        Zlib::GzipReader.open(@file_path) do |tar_file|
          Archive::Tar::Minitar.open(tar_file) do |tar_file_entries|
            tar_file_entries.each do |entry|
              entry_pathname = Pathname.new(entry.name)
              strip_pathname = Pathname.new(@options[:strip_path] || '')
              target_pathname = entry_pathname.relative_path_from(strip_pathname)

              file_path = File.join(@extract_path, target_pathname)
              FileUtils.mkdir_p(File.dirname(file_path))
              if entry.file? && !File.exist?(file_path)
                File.open(file_path, 'w', entry.mode) { |f| f.write(entry.read) }
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rake-dependencies-0.11.0 lib/rake_dependencies/extractors.rb
rake-dependencies-0.10.0 lib/rake_dependencies/extractors.rb
rake-dependencies-0.9.0 lib/rake_dependencies/extractors.rb
rake-dependencies-0.8.0 lib/rake_dependencies/extractors.rb
rake-dependencies-0.7.0 lib/rake_dependencies/extractors.rb
rake-dependencies-0.6.1 lib/rake_dependencies/extractors.rb
rake-dependencies-0.6.0 lib/rake_dependencies/extractors.rb