Class: Tarchiver::Archiver

Inherits:
Object
  • Object
show all
Defined in:
lib/tarchiver/archiver.rb

Class Method Summary collapse

Class Method Details

.archive(archive_input, output_directory = '.', opts = {}) ⇒ Object



7
8
9
10
11
12
13
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
# File 'lib/tarchiver/archiver.rb', line 7

def self.archive(archive_input, output_directory='.', opts={})
  messages = Tarchiver::Constants::MESSAGES
  # Sanitize input
  options = Tarchiver::Helpers.sanitize_options(Tarchiver::Constants::DEFAULT_ARCHIVE_OPTIONS.merge(opts))
  archive_name, relative_to, to_archive = Tarchiver::Helpers.sanitize_input(archive_input, options)

  return Tarchiver::Helpers.terminate(nil, options) unless archive_name
  archive_path = ::File.join(output_directory, archive_name)
  
  # Prepare for tarballing
  puts messages[:start_archiving] if options[:verbose]
  puts messages[:start_tarballing] if options[:verbose]
  Tarchiver::Helpers.prepare_for_tarchiving(archive_path)
  tar_path = Tarchiver::Tarballer.tar(to_archive, archive_name, relative_to, output_directory, options)
  
  # Return on failure
  return Tarchiver::Helpers.terminate(nil, options) unless tar_path
  
  puts messages[:done] if options[:verbose]

  # Intermittent cleanup
  Tarchiver::Helpers.cleanup(archive_input, nil, options)
  
  # Return if no compression was requested
  
  return tar_path if options[:archive_type] == :tar
  
  # Compress
  puts messages[:start_compressing] if options[:verbose]
  compressed_archive_path = options[:compressor].compress(archive_path, tar_path, options)
  puts messages[:done] if options[:verbose]
  
  # Cleanup
  puts messages[:start_cleaning] if options[:verbose]
  Tarchiver::Helpers.cleanup(archive_input, tar_path, options)
  puts messages[:done] if options[:verbose]
  puts messages[:completed_archiving] if options[:verbose]
  
  # Return
  ::File.exists?(compressed_archive_path) ? compressed_archive_path : Tarchiver::Helpers.terminate(nil, options)
end

.unarchive(archive, output_directory = '.', opts = {}) ⇒ Object

archive



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/tarchiver/archiver.rb', line 49

def self.unarchive(archive, output_directory='.', opts={})
  options = Tarchiver::Constants::DEFAULT_UNARCHIVE_OPTIONS.merge(opts)
  archive_type = Tarchiver::Helpers.determine_archive_type(archive)
  begin
    io = case archive_type 
    when :tar
      ::File.open(archive)
    when :compressed
      options[:compressor].open(archive)
    end
  
  Gem::Package::TarReader.new(io) do |tar|
    tar.each do |entry|
     dir = ::File.join(output_directory, ::File.dirname(entry.full_name))
     path = ::File.join(output_directory, entry.full_name)
     if entry.directory?
       FileUtils.mkdir_p(dir, mode: entry.header.mode, verbose: false)
     elsif entry.header.typeflag == '2' #Symlink!
        ::File.symlink(entry.header.linkname, path) 
     elsif entry.file?
       FileUtils.mkdir_p(dir, verbose: false) unless ::File.directory?(dir)
       ::File.open(path, "wb") do |file|
         while buffer = entry.read(options[:blocksize])
           file.write(buffer)
         end
       end
       FileUtils.chmod(entry.header.mode, path, verbose: false)
     end
    end
  end
  ::File.delete(archive) if ::File.exists?(archive) && options[:delete_input_on_success]
  output_directory
  rescue => error
   puts "#{messages[:failed_archiving]}\n#{error.message}" if options[:verbose]
   Tarchiver::Helpers.terminate(error, options)
  end
end