Created
July 14, 2018 10:37
-
-
Save ForeverZer0/2adbba36fd452738e7cca6a63aee2f30 to your computer and use it in GitHub Desktop.
Unpacks a tar.gz file using Ruby, with symbolic-link and long name support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems/package' | |
require 'zlib' | |
TAR_LONGLINK = '././@LongLink' | |
tar_gz_archive = '/path/to/archive.tar.gz' | |
destination = '/where/extract/to' | |
Gem::Package::TarReader.new( Zlib::GzipReader.open tar_gz_archive ) do |tar| | |
dest = nil | |
tar.each do |entry| | |
if entry.full_name == TAR_LONGLINK | |
dest = File.join destination, entry.read.strip | |
next | |
end | |
dest ||= File.join destination, entry.full_name | |
if entry.directory? | |
FileUtils.rm_rf dest unless File.directory? dest | |
FileUtils.mkdir_p dest, :mode => entry.header.mode, :verbose => false | |
elsif entry.file? | |
FileUtils.rm_rf dest unless File.file? dest | |
File.open dest, "wb" do |f| | |
f.print entry.read | |
end | |
FileUtils.chmod entry.header.mode, dest, :verbose => false | |
elsif entry.header.typeflag == '2' #Symlink! | |
File.symlink entry.header.linkname, dest | |
end | |
dest = nil | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment