Module: CardanoUp::Tar

Defined in:
lib/cardano-up/tar.rb

Overview

unpacking archives

Class Method Summary collapse

Class Method Details

.ungzip(tarfile) ⇒ Object

un-gzips the given IO, returning the decompressed version as a StringIO



19
20
21
22
23
24
# File 'lib/cardano-up/tar.rb', line 19

def self.ungzip(tarfile)
  z = Zlib::GzipReader.new(tarfile)
  unzipped = StringIO.new(z.read)
  z.close
  unzipped
end

.untar(io, destination) ⇒ Object

untars the given IO into the specified directory



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cardano-up/tar.rb', line 28

def self.untar(io, destination)
  Gem::Package::TarReader.new io do |tar|
    tar.each do |tarfile|
      destination_file = File.join destination, tarfile.full_name

      if tarfile.directory?
        FileUtils.mkdir_p destination_file
      else
        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
end

.unzip(file, destination) ⇒ Object

unzip file to destination directory



7
8
9
10
11
12
13
14
15
# File 'lib/cardano-up/tar.rb', line 7

def self.unzip(file, destination)
  FileUtils.mkdir_p(destination)
  Zip::File.open(file) do |zip_file|
    zip_file.each do |f|
      fpath = File.join(destination, f.name)
      zip_file.extract(f, fpath)
    end
  end
end