Class: Tarchiver::Archiver
- Inherits:
-
Object
- Object
- Tarchiver::Archiver
- Defined in:
- lib/tarchiver/archiver.rb
Class Method Summary collapse
- .archive(archive_input, output_directory = '.', opts = {}) ⇒ Object
-
.unarchive(archive, output_directory = '.', opts = {}) ⇒ Object
archive.
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={}) = Tarchiver::Constants::MESSAGES # Sanitize input = Tarchiver::Helpers.(Tarchiver::Constants::DEFAULT_ARCHIVE_OPTIONS.merge(opts)) archive_name, relative_to, to_archive = Tarchiver::Helpers.sanitize_input(archive_input, ) return Tarchiver::Helpers.terminate(nil, ) unless archive_name archive_path = ::File.join(output_directory, archive_name) # Prepare for tarballing puts [:start_archiving] if [:verbose] puts [:start_tarballing] if [:verbose] Tarchiver::Helpers.prepare_for_tarchiving(archive_path) tar_path = Tarchiver::Tarballer.tar(to_archive, archive_name, relative_to, output_directory, ) # Return on failure return Tarchiver::Helpers.terminate(nil, ) unless tar_path puts [:done] if [:verbose] # Intermittent cleanup Tarchiver::Helpers.cleanup(archive_input, nil, ) # Return if no compression was requested return tar_path if [:archive_type] == :tar # Compress puts [:start_compressing] if [:verbose] compressed_archive_path = [:compressor].compress(archive_path, tar_path, ) puts [:done] if [:verbose] # Cleanup puts [:start_cleaning] if [:verbose] Tarchiver::Helpers.cleanup(archive_input, tar_path, ) puts [:done] if [:verbose] puts [:completed_archiving] if [:verbose] # Return ::File.exists?(compressed_archive_path) ? compressed_archive_path : Tarchiver::Helpers.terminate(nil, ) 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={}) = 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 [: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([:blocksize]) file.write(buffer) end end FileUtils.chmod(entry.header.mode, path, verbose: false) end end end ::File.delete(archive) if ::File.exists?(archive) && [:delete_input_on_success] output_directory rescue => error puts "#{[:failed_archiving]}\n#{error.}" if [:verbose] Tarchiver::Helpers.terminate(error, ) end end |