To extract files from a .tar.gz file you can use the following methods from packages distributed with Ruby:
require 'rubygems/package'
require 'zlib'
tar_extract = Gem::Package::TarReader.new(Zlib::GzipReader.open('Path/To/myfile.tar.gz'))
tar_extract.rewind # The extract has to be rewinded after every iteration
tar_extract.each do |entry|
puts entry.full_name
puts entry.directory?
puts entry.file?
# puts entry.read
end
tar_extract.close
Each entry of type Gem::Package::TarReader::Entry points to a file or directory within the .tar.gz file.
Similar code can be used (replace Reader with Writer) to write files to a .tar.gz file.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…