Class: Omnizip::Formats::Rar::Rar5::Writer
- Inherits:
-
Object
- Object
- Omnizip::Formats::Rar::Rar5::Writer
- Defined in:
- lib/omnizip/formats/rar/rar5/writer.rb
Overview
RAR5 format writer
This class creates RAR5 archives with STORE or LZMA compression. Supports single-file archives, multi-volume archives, solid compression, AES-256-CBC encryption, and PAR2 recovery records.
Constant Summary collapse
- RAR5_SIGNATURE =
RAR5 signature: "Rar!\x1A\x07\x01\x00"
[0x52, 0x61, 0x72, 0x21, 0x1A, 0x07, 0x01, 0x00].pack("C*")
- AUTO_COMPRESS_THRESHOLD =
Threshold for automatic compression selection (1 KB)
1024
Instance Attribute Summary collapse
-
#options ⇒ Hash
readonly
Archive options.
-
#path ⇒ String
readonly
Output archive path.
Instance Method Summary collapse
-
#add_directory(dir_path, base_path = nil) ⇒ void
Add directory recursively.
-
#add_file(input_path, archive_path = nil) ⇒ Object
Add file to archive.
-
#initialize(path, options = {}) ⇒ Writer
constructor
Initialize RAR5 writer.
-
#write ⇒ String+
Write archive to disk.
Constructor Details
#initialize(path, options = {}) ⇒ Writer
Initialize RAR5 writer
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/omnizip/formats/rar/rar5/writer.rb', line 75 def initialize(path, = {}) @path = path @options = { compression: :store, level: 3, include_mtime: false, include_crc32: false, solid: false, password: nil, kdf_iterations: 262_144, recovery: false, recovery_percent: 5, multi_volume: false, volume_size: nil, volume_naming: "part", }.merge() @files = [] # Initialize encryption manager if password provided (and not empty) @encryption_manager = if @options[:password] && !@options[:password].empty? Encryption::EncryptionManager.new( @options[:password], kdf_iterations: @options[:kdf_iterations], ) end end |
Instance Attribute Details
#options ⇒ Hash (readonly)
Returns Archive options.
57 58 59 |
# File 'lib/omnizip/formats/rar/rar5/writer.rb', line 57 def @options end |
#path ⇒ String (readonly)
Returns Output archive path.
54 55 56 |
# File 'lib/omnizip/formats/rar/rar5/writer.rb', line 54 def path @path end |
Instance Method Details
#add_directory(dir_path, base_path = nil) ⇒ void
This method returns an undefined value.
Add directory recursively
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/omnizip/formats/rar/rar5/writer.rb', line 129 def add_directory(dir_path, base_path = nil) unless File.directory?(dir_path) raise ArgumentError, "Directory not found: #{dir_path}" end base_path ||= dir_path Dir.glob(File.join(dir_path, "**", "*")).each do |path| next unless File.file?(path) relative_path = path.sub( /^#{Regexp.escape(base_path)}#{File::SEPARATOR}?/, "" ) add_file(path, relative_path) end end |
#add_file(input_path, archive_path = nil) ⇒ Object
Add file to archive
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/omnizip/formats/rar/rar5/writer.rb', line 107 def add_file(input_path, archive_path = nil) unless File.exist?(input_path) raise ArgumentError, "File not found: #{input_path}" end archive_path ||= File.basename(input_path) # Store file with metadata @files << { input: input_path, archive: archive_path, mtime: File.mtime(input_path), stat: File.stat(input_path), } end |
#write ⇒ String+
Write archive to disk
For single archives, returns the archive path or array of paths (with PAR2). For multi-volume archives, returns an array of volume paths.
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/omnizip/formats/rar/rar5/writer.rb', line 153 def write archive_paths = if @options[:multi_volume] write_multi_volume else write_single_archive [@path] end # Generate PAR2 recovery files if requested if @options[:recovery] par2_paths = generate_recovery_files(archive_paths) return archive_paths + par2_paths end @options[:multi_volume] ? archive_paths : archive_paths.first end |