Class: RosettAi::Backup::TarXzCompressor
- Inherits:
-
Compressor
- Object
- Compressor
- RosettAi::Backup::TarXzCompressor
- Defined in:
- lib/rosett_ai/backup/compressor.rb
Overview
tar.xz compression using system xz command
Constant Summary
Constants inherited from Compressor
Instance Method Summary collapse
-
#available? ⇒ Boolean
True if the
xzbinary is found on PATH. -
#compress(files, _base_dirs, output_path, level: nil) ⇒ String
The output path.
-
#extension ⇒ String
".tar.xz".
Methods inherited from Compressor
Instance Method Details
#available? ⇒ Boolean
Returns true if the xz binary is found on PATH.
138 139 140 141 142 |
# File 'lib/rosett_ai/backup/compressor.rb', line 138 def available? ENV.fetch('PATH', '').split(File::PATH_SEPARATOR).any? do |dir| File.executable?(File.join(dir, 'xz')) end end |
#compress(files, _base_dirs, output_path, level: nil) ⇒ String
Returns the output path.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/rosett_ai/backup/compressor.rb', line 155 def compress(files, _base_dirs, output_path, level: nil) tar_path = "#{output_path}.tar" File.open(tar_path, 'wb', 0o644) do |file| Gem::Package::TarWriter.new(file) do |tar| files.each do |entry| content = File.read(entry[:full_path]) tar.add_file_simple(entry[:archive_path], 0o644, content.bytesize) do |io| io.write(content) end end end end xz_args = ['xz'] xz_args.push("-#{level}") if level xz_args.push('--stdout', tar_path) success = File.open(output_path, 'wb', 0o644) do |out| system(*xz_args, out: out) end FileUtils.rm_f(tar_path) raise RosettAi::BackupError, 'xz compression failed' unless success output_path end |
#extension ⇒ String
Returns ".tar.xz".
145 146 147 |
# File 'lib/rosett_ai/backup/compressor.rb', line 145 def extension '.tar.xz' end |