PHP code example of mlocati / chm-lib

1. Go to this page and download the library: Download mlocati/chm-lib library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mlocati / chm-lib example snippets




$chm = \CHMLib\CHM::fromFile('YourFile.chm');
foreach ($chm->getEntries(\CHMLib\Entry::TYPE_FILE) as $entry) {
    echo "File: ", $entry->getPath(), "\n";
    echo "Contents: ", $entry->getContents(), "\n\n";
}


use \CHMLib\CHM;
use \CHMLib\Entry;

// Specify the output directory
$outputDirectory = 'output';

// Specify the input CHM file
$inputCHMFile = 'YourFile.chm';

) {
    echo "Processing {$entry->getPath()}... ";
    $entryPath = ltrim(str_replace('/', DIRECTORY_SEPARATOR, $entry->getPath()), DIRECTORY_SEPARATOR);
    $parts = explode(DIRECTORY_SEPARATOR, $entryPath);
    $subDirectory = count($parts) > 1 ? implode(DIRECTORY_SEPARATOR, array_splice($parts, 0, -1)) : '';
    $filename = array_pop($parts);
    $path = $outputDirectory;
    if ($subDirectory !== '') {
        $path .= DIRECTORY_SEPARATOR . $subDirectory;
        if (!is_dir($path)) {
            mkdir($path, 0777, true);
        }
    }
    $path .= DIRECTORY_SEPARATOR . $filename;
    file_put_contents($path, $entry->getContents());
    echo "done.\n";
}
echo "\nAll done.\n";



function printTree($tree, $level)
{
    if ($tree !== null) {
        foreach ($tree->getItems() as $child) {
            echo str_repeat("\t", $level).print_r($child->getName(), 1)."\n";
            printTree($child->getChildren(), $level + 1);
        }
    }
}


$chm = \CHMLib\CHM::fromFile('YourFile.chm');

$toc = $chm->getTOC(); // Parse the contents of the .hhc file
$index = $chm->getIndex(); // Parse the contents of the .hhk file

printTree($toc, 0);



$main = \CHMLib\CHM::fromFile('main.chm');
$map = new \CHMLib\Map();
$map->add('sub1.chm', \CHMLib\CHM::fromFile('sub1.chm'));
$map->add('sub2.chm', \CHMLib\CHM::fromFile('sub2.chm'));

$toc = $main->getTOC();
$toc->resolve($map);

// Now the TOC of the main CHM file contains references to the entries in the other two CHM files 
printTree($toc, 0);