class Archive::Tar::Writer

Public Class Methods

new(stream, options = {}) click to toggle source
# File lib/archive/tar/writer.rb, line 32
def initialize(stream, options = {})
  options = {
    block_size: 2 ** 19,
    format: :gnu
  }.merge(options)
  
  @options = options
  @stream = stream
  @inodes = {}
end

Public Instance Methods

add_directory(dir, options = {}) click to toggle source
# File lib/archive/tar/writer.rb, line 79
def add_directory(dir, options = {})
  options = {
    full_path: false,
    recursive: true,
    archive_base: ""
  }.merge(options)
  
  real_base = dir
  archive_base = options[:archive_base]
  
  unless dir.is_a? Dir
    dir = Dir.open(dir)
  end
  
  #add_file(dir.path, archive_base)
  
  dir.each do |entry|
    if entry == "." || entry == ".."
      next
    end
    
    realpath = File.join(real_base, entry)
    real_archive_path = join_path(archive_base, entry)
    
    add_file(realpath, real_archive_path)
    
    if File::Stat.new(realpath).directory? && options[:recursive]
      new_options = options
      new_options[:archive_base] = real_archive_path
      
      add_directory(realpath, new_options)
    end
  end
end
add_entry(header, content) click to toggle source
# File lib/archive/tar/writer.rb, line 43
def add_entry(header, content)
  @atream.write(Archive::Tar::Format::pack_header(header))
  
  content = content[0, header.size].ljust(header.blocks * 512, "\0")
  @stream.write(content)
end
add_file(file, path = nil) click to toggle source
# File lib/archive/tar/writer.rb, line 50
def add_file(file, path = nil)
  file = File.is_a?(File) ? file : File.new(file)
  path = path == nil ? file.path : path
  
  ino = File::Stat.new(file).ino
  stat = Archive::Tar::Stat::from_file(file)
  stat.path = path
  stat.format = @options[:format]
  if @inodes.has_key? ino && path != @inodes[ino]
    stat.type = :link
    stat.size = 0
    stat.dest = @inodes[ino]
  else
    @inodes[ino] = path
  end
  
  header = Archive::Tar::Format::pack_header(stat)
  @stream.write(header)
  
  if stat.type == :normal
    num_of_nils = stat.blocks * 512 - stat.size
    until file.eof?
      @stream.write(file.read(@options[:block_size]))
    end
    
    @stream.write("\0" * num_of_nils)
  end
end
close() click to toggle source
# File lib/archive/tar/writer.rb, line 114
def close()
  @stream.write("\0" * 1024)
end