Class: Omnizip::Formats::Rar::ExternalWriter
- Inherits:
-
Object
- Object
- Omnizip::Formats::Rar::ExternalWriter
- Defined in:
- lib/omnizip/formats/rar/external_writer.rb
Overview
External RAR writer using licensed WinRAR installation
This class provides RAR archive creation by wrapping the user's licensed WinRAR command-line tool. It does NOT implement RAR compression internally due to proprietary licensing restrictions.
Instance Attribute Summary collapse
-
#files ⇒ Array<String>
readonly
Files to add.
-
#options ⇒ Hash
readonly
Compression options.
-
#output_path ⇒ String
readonly
Output archive path.
Class Method Summary collapse
-
.available? ⇒ Boolean
Check if RAR creation is available.
-
.find_rar_executable ⇒ String?
Find RAR executable on system.
-
.find_unix_rar ⇒ String?
Find RAR on Unix systems.
-
.find_windows_rar ⇒ String?
Find WinRAR on Windows.
-
.info ⇒ Hash
Get RAR executable information.
-
.which(cmd) ⇒ String?
Find executable in PATH.
Instance Method Summary collapse
-
#add_directory(dir_path, recursive: true, archive_path: nil) ⇒ Object
Add directory to archive.
-
#add_file(file_path, archive_path = nil) ⇒ Object
Add file to archive.
-
#initialize(output_path, options = {}) ⇒ ExternalWriter
constructor
Initialize RAR writer.
-
#write ⇒ Object
Create RAR archive.
Constructor Details
#initialize(output_path, options = {}) ⇒ ExternalWriter
Initialize RAR writer
85 86 87 88 89 90 91 92 93 |
# File 'lib/omnizip/formats/rar/external_writer.rb', line 85 def initialize(output_path, = {}) @output_path = output_path @options = .merge() @files = [] @directories = [] validate_availability! validate_license! unless @options[:license_confirmed] end |
Instance Attribute Details
#files ⇒ Array<String> (readonly)
Returns Files to add.
35 36 37 |
# File 'lib/omnizip/formats/rar/external_writer.rb', line 35 def files @files end |
#options ⇒ Hash (readonly)
Returns Compression options.
32 33 34 |
# File 'lib/omnizip/formats/rar/external_writer.rb', line 32 def @options end |
#output_path ⇒ String (readonly)
Returns Output archive path.
29 30 31 |
# File 'lib/omnizip/formats/rar/external_writer.rb', line 29 def output_path @output_path end |
Class Method Details
.available? ⇒ Boolean
Check if RAR creation is available
40 41 42 |
# File 'lib/omnizip/formats/rar/external_writer.rb', line 40 def self.available? !find_rar_executable.nil? end |
.find_rar_executable ⇒ String?
Find RAR executable on system
64 65 66 67 68 69 70 |
# File 'lib/omnizip/formats/rar/external_writer.rb', line 64 def self.find_rar_executable if RUBY_PLATFORM.match?(/win32|mingw/) find_windows_rar else find_unix_rar end end |
.find_unix_rar ⇒ String?
Find RAR on Unix systems
290 291 292 |
# File 'lib/omnizip/formats/rar/external_writer.rb', line 290 def self.find_unix_rar which("rar") end |
.find_windows_rar ⇒ String?
Find WinRAR on Windows
273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/omnizip/formats/rar/external_writer.rb', line 273 def self.find_windows_rar # Check common installation paths paths = [ "C:\\Program Files\\WinRAR\\Rar.exe", "C:\\Program Files (x86)\\WinRAR\\Rar.exe", ] # Check PATH path_rar = which("rar.exe") || which("Rar.exe") paths.unshift(path_rar) if path_rar paths.find { |path| File.exist?(path) } end |
.info ⇒ Hash
Get RAR executable information
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/omnizip/formats/rar/external_writer.rb', line 47 def self.info exe = find_rar_executable return { available: false } unless exe version_output, = Open3.capture2e(exe) version = version_output.match(/RAR\s+([\d.]+)/i)&.[](1) || "unknown" { available: true, executable: exe, version: version, } end |
.which(cmd) ⇒ String?
Find executable in PATH
298 299 300 301 302 303 304 305 306 307 |
# File 'lib/omnizip/formats/rar/external_writer.rb', line 298 def self.which(cmd) exts = ENV["PATHEXT"] ? ENV["PATHEXT"].split(";") : [""] ENV["PATH"].split(File::PATH_SEPARATOR).each do |path| exts.each do |ext| exe = File.join(path, "#{cmd}#{ext}") return exe if File.executable?(exe) && !File.directory?(exe) end end nil end |
Instance Method Details
#add_directory(dir_path, recursive: true, archive_path: nil) ⇒ Object
Add directory to archive
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/omnizip/formats/rar/external_writer.rb', line 116 def add_directory(dir_path, recursive: true, archive_path: nil) raise ArgumentError, "Directory not found: #{dir_path}" unless Dir.exist?(dir_path) @directories << { source: File.(dir_path), recursive: recursive, archive_path: archive_path, } end |
#add_file(file_path, archive_path = nil) ⇒ Object
Add file to archive
100 101 102 103 104 105 106 107 108 |
# File 'lib/omnizip/formats/rar/external_writer.rb', line 100 def add_file(file_path, archive_path = nil) raise ArgumentError, "File not found: #{file_path}" unless File.exist?(file_path) @files << { source: File.(file_path), archive_path: archive_path, } end |
#write ⇒ Object
Create RAR archive
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/omnizip/formats/rar/external_writer.rb', line 131 def write raise RarNotAvailableError unless self.class.available? # Build command cmd = build_command # Execute RAR stdout, stderr, status = Open3.capture3(*cmd) unless status.success? raise "RAR creation failed: #{stderr}\n#{stdout}" end # Test archive if requested test_archive if @options[:test_after_create] @output_path end |