Class: Omnizip::Formats::Rar::Writer

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/omnizip/formats/rar/writer.rb

Overview

Pure Ruby RAR archive writer

This class provides basic RAR archive creation in pure Ruby. It writes RAR4-compatible archives with basic compression support.

Examples:

Create a RAR archive

writer = Writer.new('archive.rar')
writer.add_file('document.pdf')
writer.add_directory('photos/')
writer.write

Create with options

writer = Writer.new('archive.rar',
  compression: :best,
  solid: true,
  recovery: 5
)

Constant Summary

Constants included from Constants

Constants::ARCHIVE_AUTH_INFO, Constants::ARCHIVE_COMMENT, Constants::ARCHIVE_ENCRYPTED, Constants::ARCHIVE_FIRST_VOLUME, Constants::ARCHIVE_LOCKED, Constants::ARCHIVE_NEW_NAMING, Constants::ARCHIVE_RECOVERY, Constants::ARCHIVE_SOLID, Constants::ARCHIVE_VOLUME, Constants::BLOCK_ARCHIVE, Constants::BLOCK_COMMENT, Constants::BLOCK_ENDARC, Constants::BLOCK_FILE, Constants::BLOCK_MARKER, Constants::BLOCK_OLD_AUTH, Constants::BLOCK_OLD_EXTRA, Constants::BLOCK_OLD_RECOVERY, Constants::BLOCK_OLD_SUBBLOCK, Constants::BLOCK_SUBBLOCK, Constants::FILE_COMMENT, Constants::FILE_DIRECTORY, Constants::FILE_ENCRYPTED, Constants::FILE_EXT_TIME, Constants::FILE_LARGE, Constants::FILE_SALT, Constants::FILE_SOLID, Constants::FILE_SPLIT_AFTER, Constants::FILE_SPLIT_BEFORE, Constants::FILE_UNICODE, Constants::FILE_VERSION, Constants::METHOD_BEST, Constants::METHOD_FAST, Constants::METHOD_FASTEST, Constants::METHOD_GOOD, Constants::METHOD_NORMAL, Constants::METHOD_STORE, Constants::OS_BEOS, Constants::OS_MACOS, Constants::OS_MSDOS, Constants::OS_OS2, Constants::OS_UNIX, Constants::OS_WIN32, Constants::RAR4_SIGNATURE, Constants::RAR5_FLAG_CHILD_BLOCKS, Constants::RAR5_FLAG_DATA_AREA, Constants::RAR5_FLAG_DATA_INHERITED, Constants::RAR5_FLAG_EXTRA_AREA, Constants::RAR5_FLAG_IS_DIR, Constants::RAR5_FLAG_MULTI_VOLUME, Constants::RAR5_FLAG_UNKNOWN_BLOCKS, Constants::RAR5_HEADER_ENCRYPTION, Constants::RAR5_HEADER_END, Constants::RAR5_HEADER_FILE, Constants::RAR5_HEADER_MAIN, Constants::RAR5_HEADER_SERVICE, Constants::RAR5_SIGNATURE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output_path, options = {}) ⇒ Writer

Initialize RAR writer

Parameters:

  • output_path (String)

    Output RAR file path

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

    Compression options

Options Hash (options):

  • :compression (Symbol)

    Compression level (:store, :fastest, :fast, :normal, :good, :best)

  • :solid (Boolean)

    Create solid archive

  • :recovery (Integer)

    Recovery record percentage (0-10)

  • :encrypt_headers (Boolean)

    Encrypt file names

  • :password (String)

    Archive password

  • :volume_size (Integer)

    Split into volumes (bytes)

  • :test_after_create (Boolean)

    Test archive after creation



72
73
74
75
76
77
# File 'lib/omnizip/formats/rar/writer.rb', line 72

def initialize(output_path, options = {})
  @output_path = output_path
  @options = default_options.merge(options)
  @files = []
  @directories = []
end

Instance Attribute Details

#directoriesArray<Hash> (readonly)

Returns Directories to add.

Returns:

  • (Array<Hash>)

    Directories to add



40
41
42
# File 'lib/omnizip/formats/rar/writer.rb', line 40

def directories
  @directories
end

#filesArray<Hash> (readonly)

Returns Files to add.

Returns:

  • (Array<Hash>)

    Files to add



37
38
39
# File 'lib/omnizip/formats/rar/writer.rb', line 37

def files
  @files
end

#optionsHash (readonly)

Returns Compression options.

Returns:

  • (Hash)

    Compression options



34
35
36
# File 'lib/omnizip/formats/rar/writer.rb', line 34

def options
  @options
end

#output_pathString (readonly)

Returns Output archive path.

Returns:

  • (String)

    Output archive path



31
32
33
# File 'lib/omnizip/formats/rar/writer.rb', line 31

def output_path
  @output_path
end

Class Method Details

.available?Boolean

Check if RAR creation is available

Returns:

  • (Boolean)

    always true for pure Ruby implementation



45
46
47
# File 'lib/omnizip/formats/rar/writer.rb', line 45

def self.available?
  true
end

.infoHash

Get RAR writer information

Returns:

  • (Hash)

    Writer type and version



52
53
54
55
56
57
58
# File 'lib/omnizip/formats/rar/writer.rb', line 52

def self.info
  {
    available: true,
    type: :pure_ruby,
    version: "4.0",
  }
end

Instance Method Details

#add_directory(dir_path, recursive: true, archive_path: nil) ⇒ Object

Add directory to archive

Parameters:

  • dir_path (String)

    Path to directory

  • recursive (Boolean) (defaults to: true)

    Include subdirectories

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

    Path within archive

Raises:

  • (ArgumentError)

    if directory does not exist



100
101
102
103
104
105
106
107
108
109
# File 'lib/omnizip/formats/rar/writer.rb', line 100

def add_directory(dir_path, recursive: true, archive_path: nil)
  raise ArgumentError, "Directory not found: #{dir_path}" unless
    Dir.exist?(dir_path)

  @directories << {
    source: File.expand_path(dir_path),
    recursive: recursive,
    archive_path: archive_path,
  }
end

#add_file(file_path, archive_path = nil) ⇒ Object

Add file to archive

Parameters:

  • file_path (String)

    Path to file

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

    Path within archive

Raises:

  • (ArgumentError)

    if file does not exist



84
85
86
87
88
89
90
91
92
# File 'lib/omnizip/formats/rar/writer.rb', line 84

def add_file(file_path, archive_path = nil)
  raise ArgumentError, "File not found: #{file_path}" unless
    File.exist?(file_path)

  @files << {
    source: File.expand_path(file_path),
    archive_path: archive_path,
  }
end

#writeString

Create RAR archive

Returns:

  • (String)

    Path to created archive



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/omnizip/formats/rar/writer.rb', line 114

def write
  File.open(@output_path, "wb") do |io|
    write_signature(io)
    write_marker_block(io)
    write_archive_header(io)
    write_file_entries(io)
    write_end_block(io)
  end

  # Test archive if requested
  test_archive if @options[:test_after_create]

  @output_path
end