Class: SolrWrapper::TgzExtractor
- Inherits:
-
Object
- Object
- SolrWrapper::TgzExtractor
- Defined in:
- lib/solr_wrapper/tgz_extractor.rb
Constant Summary collapse
- TAR_LONGLINK =
'././@LongLink'
Instance Attribute Summary collapse
-
#destination ⇒ Object
readonly
Returns the value of attribute destination.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
Instance Method Summary collapse
- #extract! ⇒ Object
-
#initialize(file, destination: nil) ⇒ TgzExtractor
constructor
A new instance of TgzExtractor.
Constructor Details
#initialize(file, destination: nil) ⇒ TgzExtractor
Returns a new instance of TgzExtractor.
10 11 12 13 |
# File 'lib/solr_wrapper/tgz_extractor.rb', line 10 def initialize(file, destination: nil) @file = file @destination = destination || Dir.mktmpdir end |
Instance Attribute Details
#destination ⇒ Object (readonly)
Returns the value of attribute destination.
6 7 8 |
# File 'lib/solr_wrapper/tgz_extractor.rb', line 6 def destination @destination end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
6 7 8 |
# File 'lib/solr_wrapper/tgz_extractor.rb', line 6 def file @file end |
Instance Method Details
#extract! ⇒ Object
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 |
# File 'lib/solr_wrapper/tgz_extractor.rb', line 15 def extract! Gem::Package::TarReader.new(Zlib::GzipReader.open(file)) 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? File.delete dest if File.file? dest FileUtils.mkdir_p dest, mode: entry.header.mode, verbose: false elsif entry.file? FileUtils.rm_rf dest if File.directory? 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 rescue StandardError => e abort "Unable to extract #{file} into #{destination}: #{e.message}" end |