Class: Omnizip::Formats::Rar5::Writer

Inherits:
Omnizip::Formats::Rar::RarFormatBase show all
Defined in:
lib/omnizip/formats/rar5/writer.rb

Overview

RAR v5 archive writer

Examples:

Writing a RAR5 archive

writer = Rar5::Writer.new
File.open("archive.rar", "wb") do |file|
  entries = [
    {name: "file.txt", data: "content", time: Time.now}
  ]
  writer.write_archive(file, entries)
end

Instance Attribute Summary

Attributes inherited from Omnizip::Formats::Rar::RarFormatBase

#spec, #version

Instance Method Summary collapse

Methods inherited from Omnizip::Formats::Rar::RarFormatBase

#block_type_code, #block_type_name, #compress, #compression_method_code, #compression_method_name, #decompress, #dictionary_size_code, #encryption_algorithm, #read_archive, #supports_feature?, #verify_magic_bytes

Constructor Details

#initializeWriter

Initialize a RAR v5 writer



21
22
23
# File 'lib/omnizip/formats/rar5/writer.rb', line 21

def initialize
  super("rar5")
end

Instance Method Details

#write_archive(io, entries) ⇒ void

This method returns an undefined value.

Write a RAR v5 archive

Delegates to the primary Formats::Rar::Rar5::Writer so the output is spec-conformant and unrar-verified: entries spill to a temporary file, the primary writer produces the archive, and the bytes are copied into the given IO. Compression methods above :store are not official-RAR compatible, so the primary writer stores them (with a warning) rather than emitting an undecodable stream.

Parameters:

  • io (IO)

    The output stream

  • entries (Array<Hash>)

    The entries to write



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/omnizip/formats/rar5/writer.rb', line 38

def write_archive(io, entries)
  Dir.mktmpdir("omnizip_rar5_write") do |tmp|
    archive_path = File.join(tmp, "archive.rar")
    primary = Rar::Rar5::Writer.new(archive_path,
                                    include_mtime: true,
                                    include_crc32: true)

    entries.each do |entry|
      name = entry[:name] || entry["name"]
      data = entry[:data] || entry["data"]
      mtime = entry[:time] || entry["time"] || Time.now

      file_path = File.join(tmp, "entries", name)
      FileUtils.mkdir_p(File.dirname(file_path))
      File.binwrite(file_path, data.to_s)
      File.utime(mtime, mtime, file_path)

      primary.add_file(file_path, name)
    end

    primary.write
    io.write(File.binread(archive_path))
  end
end