Module: Wordlist::Compression::Writer

Defined in:
lib/wordlist/compression/writer.rb

Overview

Handles writing compressed files.

Since:

  • 1.0.0

Constant Summary collapse

COMMANDS =

Mapping of compression formats to the commands to write to them.

Since:

  • 1.0.0

{
  gzip:  'gzip',
  bzip2: 'bzip2',
  xz:    'xz'
}

Class Method Summary collapse

Class Method Details

.command(path, format:, append: false) ⇒ String

Returns the command to write to the compressed wordlist.

Parameters:

  • path (String)

    The path to the file.

  • format (:gzip, :bzip2, :xz)

    The compression format of the file.

  • append (Boolean) (defaults to: false)

    Indicates that new words should be appended to the file instead of overwriting the file.

Returns:

  • (String)

    The shellescaped command string.

Raises:

  • (UnknownFormat)

    The given format was not :gzip, :bzip2, or :xz.

Since:

  • 1.0.0



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/wordlist/compression/writer.rb', line 39

def self.command(path, format: , append: false)
  command  = COMMANDS.fetch(format) do
    raise(UnknownFormat,"unsupported format: #{format.inspect}")
  end

  redirect = if append then '>>'
             else           '>'
             end

  return "#{Shellwords.shellescape(command)} #{redirect} #{Shellwords.shellescape(path)}"
end

.open(path, **kwargs) ⇒ IO

Opens the compressed wordlist for reading.

Parameters:

  • path (String)

    The path to the file.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for command.

Returns:

  • (IO)

    The uncompressed IO stream.

Raises:

  • (ArgumentError)

    The given format was not :gzip, :bzip2, or :xz.

  • (CommandNotFound)

    The gzip, bzip2, or xz command was not found on the system.

Since:

  • 1.0.0



69
70
71
72
73
74
75
76
77
# File 'lib/wordlist/compression/writer.rb', line 69

def self.open(path,**kwargs)
  command = self.command(path,**kwargs)

  begin
    IO.popen(command,'w')
  rescue Errno::ENOENT
    raise(CommandNotFound,"#{command.inspect} command not found")
  end
end