Class: CbzTools::CbrConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/cbz_tools/cbr_converter.rb,
sig/cbz_tools.rbs

Overview

Converts a CBR (Comic Book RAR) archive into a CBZ (Comic Book ZIP) archive.

Extraction is handled internally using the unrar command-line tool, which must be installed on the system. Consumers do not need to install or configure any extraction backend.

Examples:

Single-shot conversion

CbzTools::CbrConverter.convert("path/to/comic.cbr", to: "path/to/comic.cbz")

Reusing one converter across many conversions

converter = CbzTools::CbrConverter.new
paths.each { |p| converter.convert(p, to: p.sub(/\.cbr\z/i, ".cbz")) }

Defined Under Namespace

Classes: ArchiveError, ExtractionError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCbrConverter

Returns a new instance of CbrConverter.

Parameters:

  • extractor: (Object)


24
# File 'sig/cbz_tools.rbs', line 24

def initialize: (?extractor: untyped) -> void

Instance Attribute Details

#extractorObject (readonly)

Returns the value of attribute extractor.

Returns:

  • (Object)


14
15
16
# File 'sig/cbz_tools.rbs', line 14

def extractor
  @extractor
end

Class Method Details

.convert(cbr_path, to:, overwrite: true, work_in: nil) ⇒ String

Returns the destination CBZ path.

Parameters:

  • cbr_path (String)

    source CBR path

  • to (String)

    destination CBZ path

  • overwrite (Boolean) (defaults to: true)

    when false, raise ArchiveError if to already exists; default true

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

    work directory; when nil (default), a fresh Dir.mktmpdir is created and cleaned up. When set, the converter uses that directory and leaves cleanup to the caller (useful when batching many conversions).

Returns:

  • (String)

    the destination CBZ path

Raises:

  • (ArchiveError)

    when target cannot be written or unrar is unavailable

See Also:



41
42
43
# File 'lib/cbz_tools/cbr_converter.rb', line 41

def self.convert(cbr_path, to:, overwrite: true, work_in: nil)
  new.convert(cbr_path, to: to, overwrite: overwrite, work_in: work_in)
end

Instance Method Details

#convert(cbr_path, to:, overwrite: true, work_in: nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cbz_tools/cbr_converter.rb', line 45

def convert(cbr_path, to:, overwrite: true, work_in: nil)
  ensure_target_slot!(to, overwrite: overwrite)
  File.delete(to) if overwrite && File.exist?(to)

  in_workdir(work_in) do |temp_dir|
    extract(cbr_path, temp_dir)
    repack(temp_dir, to)
  end

  to
end

#ensure_extractor!void

This method returns an undefined value.



35
# File 'sig/cbz_tools.rbs', line 35

def ensure_extractor!: -> void

#ensure_target_slot!(to, overwrite:) ⇒ void

This method returns an undefined value.

Parameters:

  • to (String)
  • overwrite: (Boolean)

Raises:



72
73
74
75
76
# File 'lib/cbz_tools/cbr_converter.rb', line 72

def ensure_target_slot!(to, overwrite:)
  return if overwrite || !File.exist?(to)

  raise ArchiveError, "Target #{to} already exists (pass overwrite: true to replace)"
end

#in_workdir(work_in) {|arg0| ... } ⇒ void

This method returns an undefined value.

Parameters:

  • work_in (String, nil)

Yields:

Yield Parameters:

  • arg0 (String)

Yield Returns:

  • (void)


78
79
80
81
82
83
84
85
# File 'lib/cbz_tools/cbr_converter.rb', line 78

def in_workdir(work_in, &block)
  if work_in
    FileUtils.mkdir_p(work_in)
    yield work_in
  else
    Dir.mktmpdir(&block)
  end
end

#repack(source_dir, target_path) ⇒ void

This method returns an undefined value.

Parameters:

  • source_dir (String)
  • target_path (String)


87
88
89
90
91
92
93
# File 'lib/cbz_tools/cbr_converter.rb', line 87

def repack(source_dir, target_path)
  Zip::File.open(target_path, create: true) do |zipfile|
    each_file_in(source_dir) { |file| add_to_zip(zipfile, source_dir, file) }
  end
rescue Zip::Error, SystemCallError => e
  raise ArchiveError, "Failed to write CBZ #{target_path}: #{e.class}: #{e.message}"
end