Class: Cabriolet::LIT::Compressor

Inherits:
Object
  • Object
show all
Defined in:
lib/cabriolet/lit/compressor.rb

Overview

Compressor creates LIT eBook files

LIT files are Microsoft Reader eBook files that use LZX compression. The compressor allows adding multiple files to create a LIT archive.

NOTE: This implementation creates non-encrypted LIT files only. DES encryption (DRM protection) is not implemented.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io_system = nil, algorithm_factory = nil) ⇒ Compressor

Initialize a new LIT compressor

Parameters:

  • io_system (System::IOSystem, nil) (defaults to: nil)

    Custom I/O system or nil for default

  • algorithm_factory (AlgorithmFactory, nil) (defaults to: nil)

    Custom algorithm factory or nil for default



27
28
29
30
31
# File 'lib/cabriolet/lit/compressor.rb', line 27

def initialize(io_system = nil, algorithm_factory = nil)
  @io_system = io_system || System::IOSystem.new
  @algorithm_factory = algorithm_factory || Cabriolet.algorithm_factory
  @files = []
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



21
22
23
# File 'lib/cabriolet/lit/compressor.rb', line 21

def files
  @files
end

#io_systemObject (readonly)

Returns the value of attribute io_system.



21
22
23
# File 'lib/cabriolet/lit/compressor.rb', line 21

def io_system
  @io_system
end

Instance Method Details

#add_file(source_path, lit_path, **options) ⇒ void

This method returns an undefined value.

Add a file to the LIT archive

Parameters:

  • source_path (String)

    Path to the source file

  • lit_path (String)

    Path within the LIT archive

  • options (Hash)

    Options for the file

Options Hash (**options):

  • :compress (Boolean)

    Whether to compress the file (default: true)



40
41
42
43
44
45
46
47
48
# File 'lib/cabriolet/lit/compressor.rb', line 40

def add_file(source_path, lit_path, **options)
  compress = options.fetch(:compress, true)

  @files << {
    source: source_path,
    lit_path: lit_path,
    compress: compress,
  }
end

#generate(output_file, **options) ⇒ Integer

Generate the LIT archive

Parameters:

  • output_file (String)

    Path to output LIT file

  • options (Hash)

    Generation options

Options Hash (**options):

  • :version (Integer)

    LIT format version (default: 1)

  • :language_id (Integer)

    Language ID (default: 0x409 English)

  • :creator_id (Integer)

    Creator ID (default: 0)

Returns:

  • (Integer)

    Bytes written to output file

Raises:

  • (Errors::CompressionError)

    if compression fails



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cabriolet/lit/compressor.rb', line 59

def generate(output_file, **options)
  version = options.fetch(:version, 1)
  language_id = options.fetch(:language_id, 0x409)
  creator_id = options.fetch(:creator_id, 0)

  raise ArgumentError, "No files added to archive" if @files.empty?
  raise ArgumentError, "Version must be 1" unless version == 1

  # Prepare file data
  file_data = prepare_files

  # Build LIT structure
  structure_builder = StructureBuilder.new(
    io_system: @io_system,
    version: version,
    language_id: language_id,
    creator_id: creator_id,
  )
  lit_structure = structure_builder.build(file_data)

  # Write to output file
  output_handle = @io_system.open(output_file, Constants::MODE_WRITE)
  begin
    bytes_written = write_lit_file(output_handle, lit_structure)
    bytes_written
  ensure
    @io_system.close(output_handle)
  end
end