Class: Ronin::Support::Archive::Zip::Reader

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ronin/support/archive/zip/reader.rb,
lib/ronin/support/archive/zip/reader/entry.rb,
lib/ronin/support/archive/zip/reader/statistics.rb

Overview

Note:

This provides a simple interface for reading zip archives using the unzip command. If you need something more powerful, use the [archive-zip] gem instead.

Handles reading zip archives.

Since:

  • 1.0.0

Defined Under Namespace

Classes: Entry, Statistics

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#map_hash

Constructor Details

#initialize(path, password: nil) {|zip| ... } ⇒ Reader

Initializes the zip archive reader.

Parameters:

  • path (String)

    The path to the zip archive file.

  • password (String, nil) (defaults to: nil)

    Optional password to use when reading the zip archive.

Yields:

  • (zip)

    If a block is given, it will be yielded the new zip reader.

Yield Parameters:

  • zip (Reader)

    The new zip reacher.

Since:

  • 1.0.0



71
72
73
74
75
76
# File 'lib/ronin/support/archive/zip/reader.rb', line 71

def initialize(path, password: nil)
  @path     = File.expand_path(path)
  @password = password

  yield self if block_given?
end

Instance Attribute Details

#passwordString? (readonly)

The optional password to use when reading the zip archive.

Returns:

Since:

  • 1.0.0



54
55
56
# File 'lib/ronin/support/archive/zip/reader.rb', line 54

def password
  @password
end

#pathString (readonly)

The path to the zip archive that will be read.

Returns:

Since:

  • 1.0.0



49
50
51
# File 'lib/ronin/support/archive/zip/reader.rb', line 49

def path
  @path
end

Class Method Details

.open(path, **kwargs) {|zip| ... } ⇒ Object

Alias to new.

Parameters:

  • path (String)

    The path to the zip archive file.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for new.

Options Hash (**kwargs):

  • :password (String, nil)

    Optional password to use when reading the zip archive.

Yields:

  • (zip)

    If a block is given, it will be yielded the new zip reader.

Yield Parameters:

  • zip (Reader)

    The new zip reacher.

See Also:

Since:

  • 1.0.0



98
99
100
# File 'lib/ronin/support/archive/zip/reader.rb', line 98

def self.open(path,**kwargs,&block)
  new(path,**kwargs,&block)
end

Instance Method Details

#[](name) ⇒ Entry?

Finds the entry with the given name.

Parameters:

  • name (String)

    The file name to search for.

Returns:

  • (Entry, nil)

    The matching entry or nil if no entry could be found.

Since:

  • 1.0.0



157
158
159
# File 'lib/ronin/support/archive/zip/reader.rb', line 157

def [](name)
  find { |entry| entry.name == name }
end

#each {|entry| ... } ⇒ Enumerator, Statistics Also known as: list

Note:

This method actually executes the unzip -v -l ZIP command and parses it's output.

Lists the contents of the zip archive.

Yields:

  • (entry)

    If a block is given it will be passed each parsed entry in the zip archive.

Yield Parameters:

  • entry (Entry)

    An entry in the zip archive.

Returns:

  • (Enumerator, Statistics)

    If no block is given an enumerator will be returned. If a block was given, then a statistics object will be returned.

Since:

  • 1.0.0



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ronin/support/archive/zip/reader.rb', line 120

def each
  return enum_for(__method__) unless block_given?

  io = IO.popen(command_argv('-v','-l',@path))

  # skip lines until the "---------  ---------- -----   ----" line
  until io.eof?
    break if io.readline.start_with?('-')
  end

  until io.eof?
    line = io.readline(chomp: true)

    unless line.start_with?('-')
      yield parse_entry_line(line)
    else
      # reached the "---------                     -------" line
      break
    end
  end

  last_line = io.readline(chomp: true)
  return parse_statistics_line(last_line)
end

#read(name, length: nil) ⇒ String

Note:

This method actually executes the unzip -p ZIP FILE command and reads it's output.

Reads the contents of an entry from the zip archive.

Parameters:

  • name (String)

    The name of the entry to read.

  • length (Integer, nil) (defaults to: nil)

    Optional number of bytes to read.

Returns:

Since:

  • 1.0.0



177
178
179
180
181
182
183
# File 'lib/ronin/support/archive/zip/reader.rb', line 177

def read(name, length: nil)
  io = IO.popen(command_argv('-p', @path, name))

  if length then io.read(length)
  else           io.read
  end
end