Sha256: cc1bd409860c908285a413f704c584e1f131c1f05f3ea067dc07bb82cc84514e

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

require "rubygems/package"

module Linner
  class Archive

    class << self
      def tar(glob, dest)
        archived = StringIO.new
        Gem::Package::TarWriter.new(archived) do |tar|
          Dir[glob].each do |file|
            mode = File.stat(file).mode
            paths = Dir[glob.sub(/\*.*/, "")]
            relative_file = file.gsub /^#{paths.join("|")}\/?/, ""
            if File.directory?(file)
              tar.mkdir relative_file, mode
            else
              tar.add_file relative_file, mode do |tf|
                File.open(file, "rb") { |f| tf.write f.read }
              end
            end
          end
        end

        archived.rewind

        Zlib::GzipWriter.open(dest) do |gz|
          gz.write archived.string
        end
      end

      def untar(path, dest)
        extracted = Gem::Package::TarReader.new Zlib::GzipReader.open(path)

        extracted.rewind

        extracted.each do |entry|
          file = File.join dest, entry.full_name
          if entry.directory?
            FileUtils.mkdir_p file
          else
            directory = File.dirname(file)
            FileUtils.mkdir_p directory unless File.directory?(directory)
            File.open file, "wb" do |f|
              f.print entry.read
            end
          end
        end

        extracted.close
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
linner-0.8.0 lib/linner/archive.rb