Class: RosettAi::Backup::ZipCompressor

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

Overview

ZIP compression using rubyzip gem

Constant Summary

Constants inherited from Compressor

Compressor::ALGORITHMS

Instance Method Summary collapse

Methods inherited from Compressor

for

Instance Method Details

#available?Boolean

Returns true if the rubyzip gem is loadable.

Returns:

  • (Boolean)

    true if the rubyzip gem is loadable



63
64
65
66
67
68
# File 'lib/rosett_ai/backup/compressor.rb', line 63

def available?
  require 'zip'
  true
rescue LoadError
  false
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



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rosett_ai/backup/compressor.rb', line 80

def compress(files, _base_dirs, output_path, level: nil)
  require 'zip'
  compression_level = level || Zip.default_compression

  ::Zip::OutputStream.open(output_path) do |zipfile|
    files.each do |entry|
      zipfile.put_next_entry(entry[:archive_path], nil, nil, ::Zip::Entry::DEFLATED, compression_level)
      zipfile.write(File.read(entry[:full_path]))
    end
  end

  output_path
end

#extensionString

Returns ".zip".

Returns:

  • (String)

    ".zip"



71
72
73
# File 'lib/rosett_ai/backup/compressor.rb', line 71

def extension
  '.zip'
end