mlocati/chm-lib

读取CHM(微软编译HTML帮助)文件

资助包维护!
mlocati
其他

1.3.0 2020-03-31 15:14 UTC

This package is auto-updated.

Last update: 2024-08-29 03:42:05 UTC


README

Build Status Coverage Status Scrutinizer Code Quality

CHMLib - 从PHP读取CHM文件

这个PHP库可以读取CHM(微软编译HTML帮助)文件并提取其内容。

使用此库没有外部依赖:唯一的要求是PHP 5.3.3或更高版本(也支持PHP 7和HHVM)。

测试

您可以为这个库运行自己的测试(有关详细信息,请参阅此处)。在GitHub仓库中,我只包括了一个测试文件,但我已经在本地上测试了我电脑上找到的所有CHM文件(大约有400个),一切工作得都很顺利:wink:。

示例用法

分析CHM文件的内容

require_once 'CHMLib.php'; // You don't need this if you use Composer

$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";
}

将CHM文件的内容提取到本地目录

<?php
use \CHMLib\CHM;
use \CHMLib\Entry;

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

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

require_once 'CHMLib.php';

if (!is_dir($outputDirectory)) {
    mkdir($outputDirectory, 0777, true);
}
$chm = CHM::fromFile($inputCHMFile);
foreach ($chm->getEntries(Entry::TYPE_FILE) as $entry) {
    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";

解析索引和目录

require_once 'CHMLib.php'; // You don't need this if you use Composer

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);

解析多个CHM目录

某些CHM文件可能被分割成多个CHM文件。假设我们有一个主文件(main.chm),其中包含一个引用两个其他CHM文件(sub1.chmsub2.chm)的目录。

这段代码可以轻松解析

require_once 'CHMLib.php'; // You don't need this if you use Composer

$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);

你真的想表示感谢吗?

你可以给我提供每月一杯咖啡或者一次性咖啡 😉