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) ⇒ Compressor

Initialize a new LIT compressor

Parameters:

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

    Custom I/O system or nil for default



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

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

Instance Attribute Details

#filesObject

Returns the value of attribute files.



14
15
16
# File 'lib/cabriolet/lit/compressor.rb', line 14

def files
  @files
end

#io_systemObject (readonly)

Returns the value of attribute io_system.



13
14
15
# File 'lib/cabriolet/lit/compressor.rb', line 13

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)



33
34
35
36
37
38
39
40
41
# File 'lib/cabriolet/lit/compressor.rb', line 33

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)

  • :encrypt (Boolean)

    Whether to encrypt (not supported, raises error)

Returns:

  • (Integer)

    Bytes written to output file

Raises:

  • (Errors::CompressionError)

    if generation fails

  • (NotImplementedError)

    if encryption is requested



53
54
55
56
57
58
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
88
89
90
91
# File 'lib/cabriolet/lit/compressor.rb', line 53

def generate(output_file, **options)
  version = options.fetch(:version, 1)
  encrypt = options.fetch(:encrypt, false)

  if encrypt
    raise NotImplementedError,
          "DES encryption is not implemented. " \
          "LIT files will be created without encryption."
  end

  raise ArgumentError, "No files added to archive" if @files.empty?

  output_handle = @io_system.open(output_file, Constants::MODE_WRITE)

  begin
    # Prepare file data
    file_data = prepare_files

    # Write header
    header_bytes = write_header(
      output_handle,
      version,
      file_data.size,
    )

    # Write file entries
    entries_bytes = write_file_entries(output_handle, file_data)

    # Write file contents
    content_bytes = write_file_contents(
      output_handle,
      file_data,
    )

    header_bytes + entries_bytes + content_bytes
  ensure
    @io_system.close(output_handle) if output_handle
  end
end