Sha256: 3c5cd67eacbfd63d3e111253fa1366484a4e9591e166ccf9c524d716b6d60b2d

Contents?: true

Size: 1.2 KB

Versions: 6

Compression:

Stored size: 1.2 KB

Contents

require 'rubygems/package'
require 'zlib'
require 'fileutils'

# Thanks: https://gist.github.com/ForeverZer0/2adbba36fd452738e7cca6a63aee2f30
class TerraspaceBundler::Extract
  class Tar
    # tar_gz_archive = '/tmp/terraspace/bundler/stage/s3-us-west-2.amazonaws.com/demo-terraform-test/modules/example-module.tgz'
    # destination = '/tmp/terraspace/where/extract/to'
    def self.extract(tar_gz_archive, destination)
      reader = Zlib::GzipReader.open(tar_gz_archive)
      Gem::Package::TarReader.new(reader) do |tar|
        dest = nil
        tar.each do |entry|
          dest ||= File.join destination, entry.full_name
          if entry.directory?
            FileUtils.rm_rf dest unless File.directory? dest
            FileUtils.mkdir_p dest, :mode => entry.header.mode, :verbose => false
          elsif entry.file?
            FileUtils.rm_rf dest unless File.file? dest
            File.open dest, "wb" do |f|
              f.print entry.read
            end
            FileUtils.chmod entry.header.mode, dest, :verbose => false
          elsif entry.header.typeflag == '2' #Symlink!
            File.symlink entry.header.linkname, dest
          end
          dest = nil
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
terraspace-bundler-0.5.0 lib/terraspace_bundler/extract/tar.rb
terraspace-bundler-0.4.4 lib/terraspace_bundler/extract/tar.rb
terraspace-bundler-0.4.3 lib/terraspace_bundler/extract/tar.rb
terraspace-bundler-0.4.2 lib/terraspace_bundler/extract/tar.rb
terraspace-bundler-0.4.1 lib/terraspace_bundler/extract/tar.rb
terraspace-bundler-0.4.0 lib/terraspace_bundler/extract/tar.rb