479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
|
# File 'lib/archive/tar/minitar/command.rb', line 479
def call(args, opts = {}, ioe = {})
argv = []
output = nil
dest = "."
files = []
while (arg = args.shift)
case arg
when '--uncompress', '-z'
opts[:uncompress] = true
when '--pipe'
opts[:output] = ioe[:error]
output = ioe[:output]
when '--output', '-o'
dest = args.shift
else
argv << arg
end
end
if argv.size < 1
ioe[:output] << "Not enough arguments.\n\n"
CommandPattern["help"][["extract"]]
return 255
end
input = argv.shift
if '-' == input
opts[:name] = "STDIN"
input = ioe[:input]
else
opts[:name] = input
input = File.open(input, "rb")
end
if opts[:name] =~ /\.tar\.gz$|\.tgz$/ or opts[:uncompress]
input = Zlib::GzipReader.new(input)
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[:entry].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
if output.nil?
Archive::Tar::Minitar.unpack(input, dest, files, &watcher)
finisher.call
else
Archive::Tar::Minitar::Input.open(input) do |inp|
inp.each do |entry|
stats = {
:mode => entry.mode,
:mtime => entry.mtime,
:size => entry.size,
:gid => entry.gid,
:uid => entry.uid,
:current => 0,
:currinc => 0,
:entry => entry
}
if files.empty? or files.include?(entry.full_name)
if entry.directory?
puts "Directory: #{entry.full_name}"
watcher[:dir, dest, stats] unless watcher.nil?
else
puts "File: #{entry.full_name}"
watcher[:file_start, destfile, stats] unless watcher.nil?
loop do
data = entry.read(4096)
break unless data
stats[:currinc] = output.write(data)
stats[:current] += stats[:currinc]
watcher[:file_progress, name, stats] unless watcher.nil?
end
watcher[:file_done, name, stats] unless watcher.nil?
end
end
end
end
end
0
end
|