Class: Zillabyte::Common::Tar
- Inherits:
-
Object
- Object
- Zillabyte::Common::Tar
- Defined in:
- lib/zillabyte/common/tar.rb
Class Method Summary collapse
-
.gzip(tarfile) ⇒ Object
gzips the underlying string in the given StringIO, returning a new StringIO representing the compressed file.
-
.tar(path, ignore_files = []) ⇒ Object
Creates a tar file in memory recursively from the given path.
-
.ungzip(tarfile) ⇒ Object
un-gzips the given IO, returning the decompressed version as a StringIO.
-
.untar(io, destination) ⇒ Object
untars the given IO into the specified directory.
Class Method Details
.gzip(tarfile) ⇒ Object
gzips the underlying string in the given StringIO, returning a new StringIO representing the compressed file.
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/zillabyte/common/tar.rb', line 58 def self.gzip(tarfile) require("stringio") require("zlib") 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, ignore_files = []) ⇒ Object
Creates a tar file in memory recursively from the given path.
Optional second argument (ignore_files) allows file exclusion.
Returns a StringIO whose underlying String is the contents of the tar file.
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/zillabyte/common/tar.rb', line 14 def self.tar(path, ignore_files = []) require("stringio") require("rubygems/package") tarfile = StringIO.new("") ignore_regexp = if ignore_files.empty?() [] else /^(#{ignore_files.map { |ignore_file| Regexp::escape(ignore_file) }.join("|")})(?:\/|$)/ end Gem::Package::TarWriter.new(tarfile) do |tar| # If the path is a directory, then tar up the dir if(File.directory?(path)) Dir[File.join(path, "**/*")].each do |file| mode = File.stat(file).mode relative_file = file.sub /^#{Regexp::escape path}\/?/, '' is_directory = File.directory?(file) # Skip ignored files. next if relative_file =~ ignore_regexp 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 # If the path is just a single file, then just tar it up elsif !(path =~ ignore_regexp) mode = File.stat(path).mode tar.add_file path, mode do |tf| File.open(path, "rb") { |f| tf.write f.read } end end end tarfile.rewind tarfile end |
.ungzip(tarfile) ⇒ Object
un-gzips the given IO, returning the decompressed version as a StringIO
73 74 75 76 77 78 |
# File 'lib/zillabyte/common/tar.rb', line 73 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
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/zillabyte/common/tar.rb', line 82 def self.untar(io, destination) require("fileutils") require("rubygems/package") 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 |