class Archive::Tar::Minitar::Command::CommandExtract

Public Instance Methods

altname() click to toggle source
    # File lib/archive/tar/minitar/command.rb
475 def altname
476   "ex"
477 end
call(args, opts = {}, ioe = {}) click to toggle source
    # File lib/archive/tar/minitar/command.rb
479 def call(args, opts = {}, ioe = {})
480   argv    = []
481   output  = nil
482   dest    = "."
483   files   = []
484 
485   while (arg = args.shift)
486     case arg
487     when '--uncompress', '-z'
488       opts[:uncompress] = true
489     when '--pipe'
490       opts[:output] = ioe[:error]
491       output = ioe[:output]
492     when '--output', '-o'
493       dest = args.shift
494     else
495       argv << arg
496     end
497   end
498 
499   if argv.size < 1
500     ioe[:output] << "Not enough arguments.\n\n"
501     CommandPattern["help"][["extract"]]
502     return 255
503   end
504 
505   input = argv.shift
506   if '-' == input
507     opts[:name] = "STDIN"
508     input = ioe[:input]
509   else
510     opts[:name] = input
511     input = File.open(input, "rb")
512   end
513 
514   if opts[:name] =~ /\.tar\.gz$|\.tgz$/ or opts[:uncompress]
515     input = Zlib::GzipReader.new(input)
516   end
517 
518   files << argv.to_a
519   files.flatten!
520 
521   if opts[:verbose]
522     watcher = lambda do |action, name, stats|
523       opts[:output] << "#{name}\n" if action == :dir or action == :file_done
524     end
525     finisher = lambda { opts[:output] << "\n" }
526   elsif opts[:progress]
527     progress = ProgressBar.new(opts[:name], 1)
528     watcher = lambda do |action, name, stats|
529       case action
530       when :file_start, :dir
531         progress.title = File.basename(name)
532         if action == :dir
533           progress.total += 1
534           progress.inc
535         else
536           progress.total += stats[:entry].size
537         end
538       when :file_progress
539         progress.inc(stats[:currinc])
540       end
541     end
542     finisher = lambda do
543       progress.title = opts[:name]
544       progress.finish
545     end
546   else
547     watcher = nil
548     finisher = lambda { }
549   end
550 
551   if output.nil?
552     Archive::Tar::Minitar.unpack(input, dest, files, &watcher)
553     finisher.call
554   else
555     Archive::Tar::Minitar::Input.open(input) do |inp|
556       inp.each do |entry|
557         stats = {
558           :mode     => entry.mode,
559           :mtime    => entry.mtime,
560           :size     => entry.size,
561           :gid      => entry.gid,
562           :uid      => entry.uid,
563           :current  => 0,
564           :currinc  => 0,
565           :entry    => entry
566         }
567 
568         if files.empty? or files.include?(entry.full_name)
569           if entry.directory?
570             puts "Directory: #{entry.full_name}"
571             watcher[:dir, dest, stats] unless watcher.nil?
572           else
573             puts "File: #{entry.full_name}"
574             watcher[:file_start, destfile, stats] unless watcher.nil?
575             loop do
576               data = entry.read(4096)
577               break unless data
578               stats[:currinc] = output.write(data)
579               stats[:current] += stats[:currinc]
580 
581               watcher[:file_progress, name, stats] unless watcher.nil?
582             end
583             watcher[:file_done, name, stats] unless watcher.nil?
584           end
585         end
586       end
587     end
588   end
589 
590   0
591 end
help() click to toggle source
    # File lib/archive/tar/minitar/command.rb
593     def help
594       help = <<-EOH
595     minitar extract [OPTIONS] <tarfile|-> [<file>+]
596 
597 Extracts files from an existing tarfile. If the tarfile is named .tar.gz
598 or .tgz, then it will be uncompressed automatically. If the tarfile is
599 "-", then it will be read from standard input (stdin) so that minitar
600 may be piped.
601 
602 The files or directories that will be extracted from the tarfile are
603 specified after the name of the tarfile itself. Directories will be
604 processed recursively. Files must be specified in full. A file
605 "foo/bar/baz.txt" cannot simply be specified by specifying "baz.txt".
606 Any file not found will simply be skipped and an error will be reported.
607 
608 extract Options:
609     --uncompress, -z  Uncompresses the tarfile with gzip.
610     --pipe            Emits the extracted files to STDOUT for piping.
611     --output, -o      Extracts the files to the specified directory.
612 
613       EOH
614     end
name() click to toggle source
    # File lib/archive/tar/minitar/command.rb
471 def name
472   "extract"
473 end