Class: Archive::Tar::Minitar::Command::CommandExtract

Inherits:
CommandPattern
  • Object
show all
Defined in:
lib/gems/archive-tar-minitar-0.5.2/lib/archive/tar/minitar/command.rb

Instance Method Summary collapse

Methods inherited from CommandPattern

<<, [], #[], add, command, command?, default_ioe

Instance Method Details

#altnameObject



475
476
477
# File 'lib/gems/archive-tar-minitar-0.5.2/lib/archive/tar/minitar/command.rb', line 475

def altname
  "ex"
end

#call(args, opts = {}, ioe = {}) ⇒ Object



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/gems/archive-tar-minitar-0.5.2/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

#helpObject



593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
# File 'lib/gems/archive-tar-minitar-0.5.2/lib/archive/tar/minitar/command.rb', line 593

def help
  help = <<-EOH
minitar extract [OPTIONS] <tarfile|-> [<file>+]

Extracts files from an existing tarfile. If the tarfile is named .tar.gz
or .tgz, then it will be uncompressed automatically. If the tarfile is
"-", then it will be read from standard input (stdin) so that minitar
may be piped.

The files or directories that will be extracted from the tarfile are
specified after the name of the tarfile itself. Directories will be
processed recursively. Files must be specified in full. A file
"foo/bar/baz.txt" cannot simply be specified by specifying "baz.txt".
Any file not found will simply be skipped and an error will be reported.

extract Options:
--uncompress, -z  Uncompresses the tarfile with gzip.
--pipe            Emits the extracted files to STDOUT for piping.
--output, -o      Extracts the files to the specified directory.

  EOH
end

#nameObject



471
472
473
# File 'lib/gems/archive-tar-minitar-0.5.2/lib/archive/tar/minitar/command.rb', line 471

def name
  "extract"
end