Class: Cabriolet::CHM::Compressor

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

Overview

Compressor for CHM (Compiled HTML Help) files

Constant Summary collapse

GUID1 =

GUIDs used in CHM headers (same as parser)

[0x10, 0xFD, 0x01, 0x7C, 0xAA, 0x7B, 0xD0, 0x11,
0x9E, 0x0C, 0x00, 0xA0, 0xC9, 0x22, 0xE6, 0xEC].pack("C*")
GUID2 =
[0x11, 0xFD, 0x01, 0x7C, 0xAA, 0x7B, 0xD0, 0x11,
0x9E, 0x0C, 0x00, 0xA0, 0xC9, 0x22, 0xE6, 0xEC].pack("C*")
CONTENT_NAME =

System file names

"::DataSpace/Storage/MSCompressed/Content"
CONTROL_NAME =
"::DataSpace/Storage/MSCompressed/ControlData"
SPANINFO_NAME =
"::DataSpace/Storage/MSCompressed/SpanInfo"
RTABLE_NAME =
"::DataSpace/Storage/MSCompressed/Transform/" \
"{7FC28940-9D31-11D0-9B27-00A0C91E9C7C}/InstanceData/ResetTable"
LZX_FRAME_SIZE =

LZX constants

32_768
DEFAULT_CHUNK_SIZE =

Default chunk size for directory

4096

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io_system = nil) ⇒ Compressor

Initialize CHM compressor

Parameters:

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

    I/O system for file operations



37
38
39
40
41
42
43
44
# File 'lib/cabriolet/chm/compressor.rb', line 37

def initialize(io_system = nil)
  @io_system = io_system || System::IOSystem.new
  @files = []
  @timestamp = Time.now.to_i
  @language_id = 0x0409 # English (US)
  @window_bits = 16
  @window_size = 1 << @window_bits
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



32
33
34
# File 'lib/cabriolet/chm/compressor.rb', line 32

def files
  @files
end

#io_systemObject (readonly)

Returns the value of attribute io_system.



32
33
34
# File 'lib/cabriolet/chm/compressor.rb', line 32

def io_system
  @io_system
end

Instance Method Details

#add_file(source_path, chm_path, section: :compressed) ⇒ void

This method returns an undefined value.

Add a file to the CHM

Parameters:

  • source_path (String)

    Path to source file

  • chm_path (String)

    Path within CHM (must start with /)

  • section (Symbol) (defaults to: :compressed)

    :uncompressed or :compressed



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cabriolet/chm/compressor.rb', line 52

def add_file(source_path, chm_path, section: :compressed)
  unless chm_path.start_with?("/")
    raise ArgumentError,
          "CHM path must start with /"
  end
  unless File.exist?(source_path)
    raise ArgumentError,
          "Source file not found: #{source_path}"
  end

  @files << {
    source: source_path,
    chm_path: chm_path,
    section: section,
  }
end

#generate(output_file, **options) ⇒ Integer

Generate the CHM file

Parameters:

  • output_file (String)

    Path to output CHM file

  • options (Hash)

    Options

Options Hash (**options):

  • :timestamp (Integer)

    Custom timestamp

  • :language_id (Integer)

    Language ID

  • :window_bits (Integer)

    LZX window size (15-21)

Returns:

  • (Integer)

    Bytes written

Raises:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/cabriolet/chm/compressor.rb', line 77

def generate(output_file, **options)
  raise ArgumentError, "No files to compress" if @files.empty?

  @timestamp = options[:timestamp] || @timestamp
  @language_id = options[:language_id] || @language_id
  @window_bits = options[:window_bits] || 16
  @window_size = 1 << @window_bits

  # Validate window bits
  unless (15..21).cover?(@window_bits)
    raise ArgumentError,
          "window_bits must be 15-21, got #{@window_bits}"
  end

  # Open output file
  output_handle = @io_system.open(output_file, Constants::MODE_WRITE)

  begin
    # Organize files into sections
    organize_sections

    # Compress section 1 files
    compress_section1

    # Build directory structure
    build_directory

    # Calculate offsets
    calculate_offsets

    # Write CHM file
    write_chm(output_handle)

    bytes_written = output_handle.tell
    output_handle.close
    bytes_written
  rescue StandardError => e
    output_handle&.close
    FileUtils.rm_f(output_file)
    raise e
  end
end