def call(args, opts = {}, ioe = {})
argv = []
while (arg = args.shift)
case arg
when '--compress', '-z'
opts[:compress] = true
else
argv << arg
end
end
if argv.size < 2
ioe[:output] << "Not enough arguments.\n\n"
CommandPattern["help"][["create"]]
return 255
end
output = argv.shift
if '-' == output
opts[:name] = "STDOUT"
output = ioe[:output]
opts[:output] = ioe[:error]
else
opts[:name] = output
output = File.open(output, "wb")
opts[:output] = ioe[:output]
end
if opts[:name] =~ /\.tar\.gz$|\.tgz$/ or opts[:compress]
output = Zlib::GzipWriter.new(output)
end
files = []
if argv.include?("--")
files = ""
files << ioe[:input].read while not ioe[:input].eof?
files = files.split(/\r\n|\n|\r/)
args.delete("--")
end
files << argv.to_a
files.flatten!
if opts[:verbose]
watcher = lambda do |action, name, stats|
opts[:output] << "#{name}\n" if action == :dir or action == :file_done
end
finisher = lambda { opts[:output] << "\n" }
elsif opts[:progress]
progress = ProgressBar.new(opts[:name], 1)
watcher = lambda do |action, name, stats|
case action
when :file_start, :dir
progress.title = File.basename(name)
if action == :dir
progress.total += 1
progress.inc
else
progress.total += stats[:size]
end
when :file_progress
progress.inc(stats[:currinc])
end
end
finisher = lambda do
progress.title = opts[:name]
progress.finish
end
else
watcher = nil
finisher = lambda { }
end
Archive::Tar::Minitar.pack(files, output, &watcher)
finisher.call
0
ensure
output.close if output and not output.closed?
end