Class: Omnizip::Formats::Rar::Rar5::MultiVolume::VolumeManager

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/formats/rar/rar5/multi_volume/volume_manager.rb

Overview

Volume manager for multi-volume archive creation

This class coordinates the creation of multi-volume RAR5 archives by managing volume splitting, file distribution, and volume writing.

Examples:

Create multi-volume archive

manager = VolumeManager.new('archive.rar',
  max_volume_size: 10 * 1024 * 1024,  # 10 MB
  compression: :lzma,
  level: 5
)
manager.add_file('large_file.dat')
manager.create_volumes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_path, options = {}) ⇒ VolumeManager

Initialize volume manager

Parameters:

  • base_path (String)

    Base archive path (e.g., "archive.rar")

  • options (Hash) (defaults to: {})

    Options

Options Hash (options):

  • :max_volume_size (Integer)

    Maximum volume size in bytes

  • :volume_naming (String)

    Naming pattern ("part", "volume", "numeric")

  • :compression (Symbol)

    Compression method (:store, :lzma)

  • :level (Integer)

    Compression level (1-5)

  • :include_mtime (Boolean)

    Include modification time

  • :include_crc32 (Boolean)

    Include CRC32



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/omnizip/formats/rar/rar5/multi_volume/volume_manager.rb', line 44

def initialize(base_path, options = {})
  @base_path = base_path
  @volume_options = Models::VolumeOptions.new(
    max_volume_size: options[:max_volume_size] || 104_857_600,
    volume_naming: options[:volume_naming] || "part",
  )
  @volume_options.validate!

  @compression_options = {
    compression: options[:compression] || :store,
    level: options[:level] || 3,
    include_mtime: options[:include_mtime] || false,
    include_crc32: options[:include_crc32] || false,
  }

  @files = []
end

Instance Attribute Details

#base_pathString (readonly)

Returns Base archive path.

Returns:

  • (String)

    Base archive path



23
24
25
# File 'lib/omnizip/formats/rar/rar5/multi_volume/volume_manager.rb', line 23

def base_path
  @base_path
end

#compression_optionsHash (readonly)

Returns Compression options.

Returns:

  • (Hash)

    Compression options



29
30
31
# File 'lib/omnizip/formats/rar/rar5/multi_volume/volume_manager.rb', line 29

def compression_options
  @compression_options
end

#filesArray<Hash> (readonly)

Returns Files to archive.

Returns:

  • (Array<Hash>)

    Files to archive



32
33
34
# File 'lib/omnizip/formats/rar/rar5/multi_volume/volume_manager.rb', line 32

def files
  @files
end

#volume_optionsVolumeOptions (readonly)

Returns Volume options.

Returns:

  • (VolumeOptions)

    Volume options



26
27
28
# File 'lib/omnizip/formats/rar/rar5/multi_volume/volume_manager.rb', line 26

def volume_options
  @volume_options
end

Instance Method Details

#add_directory(dir_path, base_path = nil) ⇒ void

This method returns an undefined value.

Add directory recursively

Parameters:

  • dir_path (String)

    Directory path

  • base_path (String, nil) (defaults to: nil)

    Base path for relative names



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/omnizip/formats/rar/rar5/multi_volume/volume_manager.rb', line 89

def add_directory(dir_path, base_path = nil)
  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) ⇒ void

This method returns an undefined value.

Add file to archive

Parameters:

  • input_path (String)

    Path to file on disk

  • archive_path (String, nil) (defaults to: nil)

    Path within archive



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/omnizip/formats/rar/rar5/multi_volume/volume_manager.rb', line 67

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)

  @files << {
    input: input_path,
    archive: archive_path,
    mtime: File.mtime(input_path),
    stat: File.stat(input_path),
    size: File.size(input_path),
  }
end

#create_volumesArray<String>

Create multi-volume archives

Returns:

  • (Array<String>)

    Paths to created volume files



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/omnizip/formats/rar/rar5/multi_volume/volume_manager.rb', line 105

def create_volumes
  # Prepare file entries with compression
  prepared_files = prepare_files

  # Calculate file distribution across volumes
  splitter = VolumeSplitter.new(max_volume_size: @volume_options.max_volume_size)
  distribution = splitter.calculate_file_distribution(prepared_files)

  # Create each volume
  volume_paths = []
  distribution.each_with_index do |file_indices, volume_idx|
    volume_number = volume_idx + 1
    is_last = (volume_idx == distribution.size - 1)

    volume_path = VolumeWriter.volume_filename(
      @base_path,
      volume_number,
      naming: @volume_options.volume_naming,
    )

    write_volume(volume_path, volume_number, file_indices,
                 prepared_files, is_last)
    volume_paths << volume_path
  end

  volume_paths
end

#needs_splitting?Boolean

Check if archive needs splitting

Returns:

  • (Boolean)

    true if multi-volume needed



136
137
138
139
140
# File 'lib/omnizip/formats/rar/rar5/multi_volume/volume_manager.rb', line 136

def needs_splitting?
  total_size = estimate_total_size
  VolumeSplitter.needs_splitting?(total_size,
                                  @volume_options.max_volume_size)
end