module Util::Tar
Public Instance Methods
gzip(tarfile)
click to toggle source
gzips the underlying string in the given StringIO, returning a new StringIO representing the compressed file.
# File lib/util/tar.rb, line 48 def gzip(tarfile) gz = StringIO.new('') z = Zlib::GzipWriter.new(gz) z.write tarfile.string z.close # this is necessary! # z was closed to write the gzip footer, so # now we need a new StringIO StringIO.new gz.string end
tar(path)
click to toggle source
Creates a tar file in memory recursively from the given path.
Returns a StringIO whose underlying String is the contents of the tar file.
# File lib/util/tar.rb, line 24 def tar(path) tarfile = StringIO.new('') Gem::Package::TarWriter.new(tarfile) do |tar| Dir[File.join(path, '**/*')].each do |file| mode = File.stat(file).mode relative_file = file.sub /^#{Regexp::escape path}\/?/, '' 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 tarfile.rewind tarfile end
ungzip(tarfile)
click to toggle source
un-gzips the given IO, returning the decompressed version as a StringIO
# File lib/util/tar.rb, line 61 def ungzip(tarfile) z = Zlib::GzipReader.new(tarfile) unzipped = StringIO.new(z.read) z.close unzipped end
untar(io, destination)
click to toggle source
untars the given IO into the specified directory
# File lib/util/tar.rb, line 70 def 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.write tarfile.read end end end end end