Class: Indocker::TarHelper

Inherits:
Object
  • Object
show all
Includes:
SmartIoC::Iocify
Defined in:
lib/indocker/utils/tar_helper.rb

Instance Method Summary collapse

Instance Method Details

#untar(io:, destination:, ignore_wrap_directory: false) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/indocker/utils/tar_helper.rb', line 11

def untar(io:, destination:, ignore_wrap_directory: false)
  files_list = []

  Gem::Package::TarReader.new io do |tar|
    tar.each do |tarfile|
      if ignore_wrap_directory
        tarfile_full_name = tarfile.full_name.split('/')[1..-1].join('/')
      else 
        tarfile_full_name = tarfile.full_name
      end

      destination_file = File.join destination, tarfile_full_name
      
      if tarfile.directory?
        FileUtils.mkdir_p destination_file
      else
        files_list.push(tarfile_full_name)

        destination_directory = File.dirname(destination_file)
        FileUtils.mkdir_p destination_directory unless File.directory?(destination_directory)
        File.open destination_file, "wb" do |f|
          f.print tarfile.read
        end
      end
    end
  end

  files_list
end