I am using Grit (Ruby gem) to load a git repo, extract a given commit to a folder, then 'do stuff' with it. Grit supports git archive with archive_tar which retuns a 'string containing tar archive'. I would like to extract this to the filesystem using Ruby libs / gems if possible (not direct system calls) and avoid things like saving the data to an archive first and then extracting it (really looking for an efficient one-liner here).
Under the assumption that 'string containing tar archive' is equivalent to
File.open("Downloads.tar", "rb") {|a| a.read }
I am able to accomplish this using fileutils, stringio, and the minitar gem. First you will need to install minitar, which should be as simple as
gem install minitar
Since minitar does not support it's regular unpack to use a stream, we'll create our own rudimentary unpacking method.
require 'archive/tar/minitar'
require 'stringio'
require 'fileutils'
def unpack_tar(directory, string)
FileUtils.mkdir_p(directory) if !File.exist?(directory)
stringio = StringIO.new(string)
input = Archive::Tar::Minitar::Input.new(stringio)
input.each {|entry|
input.extract_entry(directory, entry)
}
end
unpack_tar("./test", File.open("Downloads.tar", "rb") {|a| a.read })
Of course replacing the whole File.open part with the archive_tar
function of the Grit gem. This is all assuming that first assumption of course, though i'm sure ths method can easily be adapted to suit whatever archive_tar
actually returns.