Class: Linner::Archive

Inherits:
Object
  • Object
show all
Defined in:
lib/linner/archive.rb

Class Method Summary collapse

Class Method Details

.tar(glob, dest) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/linner/archive.rb', line 7

def tar(glob, dest)
  archived = StringIO.new
  Gem::Package::TarWriter.new(archived) do |tar|
    Dir[glob].each do |file|
      paths = Linner.env.paths
      mode = File.stat(file).mode
      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

.untar(path, dest) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/linner/archive.rb', line 31

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