Class: Omnizip::Formats::Iso::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/formats/iso/writer.rb

Overview

ISO 9660 image writer

Creates ISO 9660 filesystem images (CD/DVD images) from files and directories. Supports ISO 9660 levels 1-3, Rock Ridge, and Joliet extensions.

Examples:

Create simple ISO image

writer = Iso::Writer.new('cdrom.iso')
writer.add_directory('files/')
writer.write

With Rock Ridge and Joliet

writer = Iso::Writer.new('backup.iso',
  volume_id: 'BACKUP_2024',
  rock_ridge: true,
  joliet: true
)
writer.add_directory('documents/')
writer.write

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initialize ISO writer

Parameters:

  • output_path (String)

    Output ISO file path

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

    Writer options

Options Hash (options):

  • :volume_id (String)

    Volume label

  • :system_id (String)

    System identifier

  • :publisher (String)

    Publisher name

  • :preparer (String)

    Preparer name

  • :application (String)

    Application name

  • :level (Integer)

    ISO 9660 level (1-3)

  • :rock_ridge (Boolean)

    Enable Rock Ridge

  • :joliet (Boolean)

    Enable Joliet



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/omnizip/formats/iso/writer.rb', line 76

def initialize(output_path, options = {})
  @output_path = output_path
  @options = default_options.merge(options)

  @volume_id = @options[:volume_id]
  @system_id = @options[:system_id]
  @publisher = @options[:publisher]
  @preparer = @options[:preparer]
  @application = @options[:application]
  @level = @options[:level]
  @rock_ridge = @options[:rock_ridge]
  @joliet = @options[:joliet]

  @files = []
  @directories = []
end

Instance Attribute Details

#applicationString

Returns Application identifier.

Returns:

  • (String)

    Application identifier



47
48
49
# File 'lib/omnizip/formats/iso/writer.rb', line 47

def application
  @application
end

#directoriesArray<Hash> (readonly)

Returns Directories to add.

Returns:

  • (Array<Hash>)

    Directories to add



62
63
64
# File 'lib/omnizip/formats/iso/writer.rb', line 62

def directories
  @directories
end

#filesArray<Hash> (readonly)

Returns Files to add.

Returns:

  • (Array<Hash>)

    Files to add



59
60
61
# File 'lib/omnizip/formats/iso/writer.rb', line 59

def files
  @files
end

#jolietBoolean

Returns Use Joliet extensions.

Returns:

  • (Boolean)

    Use Joliet extensions



56
57
58
# File 'lib/omnizip/formats/iso/writer.rb', line 56

def joliet
  @joliet
end

#levelInteger

Returns ISO 9660 level (1, 2, or 3).

Returns:

  • (Integer)

    ISO 9660 level (1, 2, or 3)



50
51
52
# File 'lib/omnizip/formats/iso/writer.rb', line 50

def level
  @level
end

#optionsHash (readonly)

Returns Writer options.

Returns:

  • (Hash)

    Writer options



32
33
34
# File 'lib/omnizip/formats/iso/writer.rb', line 32

def options
  @options
end

#output_pathString (readonly)

Returns Output ISO file path.

Returns:

  • (String)

    Output ISO file path



29
30
31
# File 'lib/omnizip/formats/iso/writer.rb', line 29

def output_path
  @output_path
end

#preparerString

Returns Preparer.

Returns:

  • (String)

    Preparer



44
45
46
# File 'lib/omnizip/formats/iso/writer.rb', line 44

def preparer
  @preparer
end

#publisherString

Returns Publisher.

Returns:

  • (String)

    Publisher



41
42
43
# File 'lib/omnizip/formats/iso/writer.rb', line 41

def publisher
  @publisher
end

#rock_ridgeBoolean

Returns Use Rock Ridge extensions.

Returns:

  • (Boolean)

    Use Rock Ridge extensions



53
54
55
# File 'lib/omnizip/formats/iso/writer.rb', line 53

def rock_ridge
  @rock_ridge
end

#system_idString

Returns System identifier.

Returns:

  • (String)

    System identifier



38
39
40
# File 'lib/omnizip/formats/iso/writer.rb', line 38

def system_id
  @system_id
end

#volume_idString

Returns Volume identifier (label).

Returns:

  • (String)

    Volume identifier (label)



35
36
37
# File 'lib/omnizip/formats/iso/writer.rb', line 35

def volume_id
  @volume_id
end

Instance Method Details

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

Add directory to ISO image

Parameters:

  • dir_path (String)

    Source directory path

  • recursive (Boolean) (defaults to: true)

    Include subdirectories

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

    Path in ISO (defaults to basename)

Raises:

  • (ArgumentError)

    if directory doesn't exist



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/omnizip/formats/iso/writer.rb', line 117

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

  iso_path ||= File.basename(dir_path)
  iso_path = sanitize_path(iso_path)

  if recursive
    add_directory_recursive(dir_path, iso_path)
  else
    add_directory_flat(dir_path, iso_path)
  end
end

#add_file(file_path, iso_path = nil) ⇒ Object

Add file to ISO image

Parameters:

  • file_path (String)

    Source file path

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

    Path in ISO (defaults to basename)

Raises:

  • (ArgumentError)

    if file doesn't exist



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

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

  iso_path ||= File.basename(file_path)

  @files << {
    source: File.expand_path(file_path),
    iso_path: sanitize_path(iso_path),
    stat: File.stat(file_path),
  }
end

#writeObject

Create ISO image

Raises:



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/omnizip/formats/iso/writer.rb', line 134

def write
  File.open(@output_path, "wb") do |io|
    # Build directory structure
    builder = DirectoryBuilder.new(
      @files,
      @directories,
      level: @level,
      rock_ridge: @rock_ridge,
    )
    dir_structure = builder.build

    # Build volume descriptor
    volume_builder = VolumeBuilder.new(
      volume_id: @volume_id,
      system_id: @system_id,
      publisher: @publisher,
      preparer: @preparer,
      application: @application,
      level: @level,
      rock_ridge: @rock_ridge,
      joliet: @joliet,
    )

    # Write ISO structure
    write_system_area(io)
    write_volume_descriptors(io, volume_builder, dir_structure)
    write_path_tables(io, dir_structure)
    write_directories(io, dir_structure)
    write_file_data(io)
  end

  @output_path
end