Class: Ronin::CLI::Commands::Unarchive Private

Inherits:
FileProcessorCommand show all
Defined in:
lib/ronin/cli/commands/unarchive.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Unarchive the file(s).

Usage

ronin unarchive [options] FILE ...

Options

-f, --format tar|zip             Explicit archive format

Arguments

FILE ...                         File(s) to unarchive

Since:

  • 2.1.0

Constant Summary collapse

ARCHIVE_FORMATS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Archive file formats and archive types.

Since:

  • 2.1.0

{
  '.tar' => :tar,
  '.zip' => :zip
}

Instance Method Summary collapse

Methods inherited from FileProcessorCommand

#open_file, #process_file, #process_input

Instance Method Details

#format_for(path) ⇒ :tar, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the format for the given archive path.

Parameters:

  • path (String)

    The path to the archive file.

Returns:

  • (:tar, :zip, nil)

    The archive format. nil is returned if the format cannot be guessed and the --format option was not given.

Since:

  • 2.1.0



92
93
94
95
96
# File 'lib/ronin/cli/commands/unarchive.rb', line 92

def format_for(path)
  options.fetch(:format) do
    ARCHIVE_FORMATS[File.extname(path)]
  end
end

#open_archive(file) {|archive_reader| ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Opens archive for read.

Zip or tar reader object.

Parameters:

  • file (String)

    File to open.

Yields:

  • (archive_reader)

    Yielded archived file.

Yield Parameters:

  • archive_reader (Ronin::Support::Archive::Zip::Reader, Ronin::Support::Archive::Tar::Reader)

Since:

  • 2.1.0



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ronin/cli/commands/unarchive.rb', line 111

def open_archive(file,&block)
  format = format_for(file)

  case format
  when :tar
    Support::Archive.untar(file,&block)
  when :zip
    Support::Archive.unzip(file,&block)
  when nil
    print_error("unknown archive format: #{file}")
  else
    raise(NotImplementedError,"archive format not supported: #{format.inspect}")
  end
end

#run(*files) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Runs the unarchive sub-command.

Parameters:

  • files (Array<String>)

    File arguments.

Since:

  • 2.1.0



66
67
68
69
70
71
72
73
74
# File 'lib/ronin/cli/commands/unarchive.rb', line 66

def run(*files)
  files.each do |file|
    open_archive(file) do |archived_files|
      archived_files.each do |archived_file|
        File.binwrite(archived_file.name, archived_file.read)
      end
    end
  end
end