Class: StaticdUtils::Tar

Inherits:
Object
  • Object
show all
Defined in:
lib/staticd_utils/tar.rb

Overview

Creation and Extraction of Tarball Stream.

Example:

tar = Tar.tar(["/tmp/hello"])
Tar.untar(tar, "/tmp")

Class Method Summary collapse

Class Method Details

.tar(files) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/staticd_utils/tar.rb', line 13

def self.tar(files)
  io = StringIO.new
  tar = Gem::Package::TarWriter.new(io)

  # Gem::Package::TarReader raise an exeption extracting an empty tarball,
  # this add at least one useless file to extract.
  tar.add_file("about", 0644) { |file| file.write("Hello.") }

  files.each do |file|
    content = File.read(file)
    sha1 = Digest::SHA1.hexdigest(content)
    tar.add_file(sha1, 0644) { |entry| entry.write(content) }
  end

  io.rewind
  io
end

.untar(io, path) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/staticd_utils/tar.rb', line 31

def self.untar(io, path)
  FileUtils.mkdir_p("#{path}")
  tar = Gem::Package::TarReader.new(io)
  tar.rewind
  tar.each do |entry|
    File.open("#{path}/#{entry.full_name}", "w+") do |file|
      file.write(entry.read)
    end
  end
  tar.close
end