Class: Omnizip::Commands::CompressCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/commands/compress_command.rb

Overview

Command to compress files using specified algorithm.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CompressCommand

Initialize compress command with options.

Parameters:

  • options (Hash) (defaults to: {})

    Command options from Thor



28
29
30
# File 'lib/omnizip/commands/compress_command.rb', line 28

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



23
24
25
# File 'lib/omnizip/commands/compress_command.rb', line 23

def options
  @options
end

Instance Method Details

#run(input_file, output_file) ⇒ void

This method returns an undefined value.

Execute the compress command.

Parameters:

  • input_file (String)

    Path to input file

  • output_file (String)

    Path to output file



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/omnizip/commands/compress_command.rb', line 37

def run(input_file, output_file)
  validate_inputs(input_file, output_file)

  algorithm_name = options[:algorithm] || "lzma"
  level = options[:level] || 5
  verbose = options[:verbose] || false

  CliOutputFormatter.verbose_puts(
    "Compressing #{input_file} to #{output_file}...",
    verbose,
  )
  CliOutputFormatter.verbose_puts(
    "Algorithm: #{algorithm_name}, Level: #{level}",
    verbose,
  )

  start_time = Time.now

  compress_file(input_file, output_file, algorithm_name, level)

  elapsed = Time.now - start_time

  input_size = File.size(input_file)
  output_size = File.size(output_file)

  if verbose
    puts ""
    puts CliOutputFormatter.format_compression_stats(
      input_size,
      output_size,
      elapsed,
    )
  else
    puts "Compressed: #{input_file} -> #{output_file}"
  end
end