Class: RosettAi::Backup::TarGzCompressor

Inherits:
Compressor
  • Object
show all
Defined in:
lib/rosett_ai/backup/compressor.rb

Overview

tar.gz compression using Ruby stdlib

Constant Summary

Constants inherited from Compressor

Compressor::ALGORITHMS

Instance Method Summary collapse

Methods inherited from Compressor

for

Instance Method Details

#available?Boolean

Returns always true (uses Ruby stdlib).

Returns:

  • (Boolean)

    always true (uses Ruby stdlib)



98
99
100
# File 'lib/rosett_ai/backup/compressor.rb', line 98

def available?
  true
end

#compress(files, _base_dirs, output_path, level: nil) ⇒ String

Returns the output path.

Parameters:

  • files (Array<Hash>)

    entries with :full_path and :archive_path keys

  • _base_dirs (Array<String>)

    unused

  • output_path (String)

    destination path for the archive

  • level (Integer, nil) (defaults to: nil)

    Zlib compression level (nil for default)

Returns:

  • (String)

    the output path



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rosett_ai/backup/compressor.rb', line 112

def compress(files, _base_dirs, output_path, level: nil)
  gz_level = level || Zlib::DEFAULT_COMPRESSION

  File.open(output_path, 'wb', 0o644) do |file|
    Zlib::GzipWriter.wrap(file, gz_level) do |gz|
      Gem::Package::TarWriter.new(gz) 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
  end

  output_path
end

#extensionString

Returns ".tar.gz".

Returns:

  • (String)

    ".tar.gz"



103
104
105
# File 'lib/rosett_ai/backup/compressor.rb', line 103

def extension
  '.tar.gz'
end