Sha256: 889717e109588f95d99e24e5f1779e73c824a0094db9d4231592e6015939b3c8

Contents?: true

Size: 1.26 KB

Versions: 35

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

require 'eac_fs/file_info'
require 'eac_ruby_utils/core_ext'

module EhbrsRubyUtils
  module Fs
    class CompressedPackage < ::EacFs::FileInfo
      MIME_TYPES = {
        'application/zip' => :zip,
        'application/x-7z-compressed' => :sevenzip,
        'application/x-rar' => :rar,
        'application/x-tar' => :tar
      }.freeze

      def extract_to(target)
        target = target.to_pathname
        target.mkpath
        sub_extract_to(target)
      end

      private

      def sub_extract_to(target)
        MIME_TYPES[content_type.mime_type].if_present do |v|
          return send("#{v}_extract_command", target).execute!
        end
        raise "Unknown how to extract \"#{path}\" (#{content_type})"
      end

      def sevenzip_extract_command(target_dir)
        ::Ehbrs::Executables.sevenzip.command('x', path, '-o', target_dir)
      end

      def tar_extract_command(target_dir)
        ::Ehbrs::Executables.tar.command('-xf', path, '-C', target_dir)
      end

      def rar_extract_command(target_dir)
        ::Ehbrs::Executables.rar.command('x', path.expand_path).chdir(target_dir)
      end

      def zip_extract_command(target_dir)
        ::Ehbrs::Executables.unzip.command(path, '-d', target_dir)
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 2 rubygems

Version Path
ehbrs_ruby_utils-0.36.1 lib/ehbrs_ruby_utils/fs/compressed_package.rb
ehbrs_ruby_utils-0.36.0 lib/ehbrs_ruby_utils/fs/compressed_package.rb
ehbrs_ruby_utils-0.35.0 lib/ehbrs_ruby_utils/fs/compressed_package.rb
ehbrs_ruby_utils-0.33.0 lib/ehbrs_ruby_utils/fs/compressed_package.rb
ehbrs_ruby_utils-0.32.0 lib/ehbrs_ruby_utils/fs/compressed_package.rb
ehbrs_ruby_utils-0.31.0 lib/ehbrs_ruby_utils/fs/compressed_package.rb
ehbrs_ruby_utils-0.30.0 lib/ehbrs_ruby_utils/fs/compressed_package.rb
ehbrs_ruby_utils-0.29.0 lib/ehbrs_ruby_utils/fs/compressed_package.rb
ehbrs_ruby_utils-0.28.0 lib/ehbrs_ruby_utils/fs/compressed_package.rb
ehbrs_ruby_utils-0.27.1 lib/ehbrs_ruby_utils/fs/compressed_package.rb
ehbrs_ruby_utils-0.27.0 lib/ehbrs_ruby_utils/fs/compressed_package.rb
ehbrs_ruby_utils-0.26.0 lib/ehbrs_ruby_utils/fs/compressed_package.rb
ehbrs_ruby_utils-0.25.1 lib/ehbrs_ruby_utils/fs/compressed_package.rb
ehbrs_ruby_utils-0.25.0 lib/ehbrs_ruby_utils/fs/compressed_package.rb
ehbrs_ruby_utils-0.24.1 lib/ehbrs_ruby_utils/fs/compressed_package.rb
ehbrs_ruby_utils-0.24.0 lib/ehbrs_ruby_utils/fs/compressed_package.rb
ehbrs_ruby_utils-0.23.0 lib/ehbrs_ruby_utils/fs/compressed_package.rb
ehbrs_ruby_utils-0.22.0 lib/ehbrs_ruby_utils/fs/compressed_package.rb
ehbrs_ruby_utils-0.21.0 lib/ehbrs_ruby_utils/fs/compressed_package.rb
ehbrs_ruby_utils-0.20.0 lib/ehbrs_ruby_utils/fs/compressed_package.rb