-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.php
156 lines (130 loc) · 4.71 KB
/
converter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
require_once 'vendor/autoload.php';
use Overtrue\PHPOpenCC\OpenCC;
class EPubConverter
{
protected $filePath;
protected $strategy;
protected $outputPath;
protected $tempDir;
public function __construct($filePath, $strategy = 's2t', $outputPath = null)
{
$this->filePath = $filePath;
$this->strategy = $strategy;
$this->outputPath = $outputPath;
$this->tempDir = sys_get_temp_dir() . '/epub_converter_' . uniqid();
}
public function convert()
{
// 1. 解压 EPUB 文件
$this->unzipEPub();
// 2. 转换内容
$this->convertContent();
// 3. 重新打包 EPUB
$this->zipEPub();
// 4. 清理临时文件
$this->cleanup();
}
protected function unzipEPub()
{
if (!is_dir($this->tempDir)) {
mkdir($this->tempDir, 0777, true);
}
$zip = new ZipArchive();
if ($zip->open($this->filePath) === true) {
$zip->extractTo($this->tempDir);
$zip->close();
} else {
throw new Exception('无法打开 EPUB 文件');
}
}
protected function convertContent()
{
// 转换元数据 (OPF 文件)
$opfFile = $this->findOPFFile();
if ($opfFile) {
$content = file_get_contents($opfFile);
$convertedContent = OpenCC::convert($content, $this->strategy);
file_put_contents($opfFile, $convertedContent);
}
// 遍历所有 HTML/XHTML 文件并转换
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($this->tempDir),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($files as $file) {
if ($file->isFile() && in_array(strtolower($file->getExtension()), ['html', 'xhtml', 'htm'])) {
$content = file_get_contents($file->getPathname());
$convertedContent = OpenCC::convert($content, $this->strategy);
file_put_contents($file->getPathname(), $convertedContent);
}
}
}
protected function findOPFFile()
{
$containerFile = $this->tempDir . '/META-INF/container.xml';
if (file_exists($containerFile)) {
$xml = simplexml_load_file($containerFile);
$rootfile = $xml->rootfiles->rootfile;
if ($rootfile && $rootfile['full-path']) {
return $this->tempDir . '/' . (string)$rootfile['full-path'];
}
}
return null;
}
protected function zipEPub()
{
$outputFile = $this->outputPath ?: $this->filePath;
$zip = new ZipArchive();
if ($zip->open($outputFile, ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) {
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($this->tempDir),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file) {
if (!$file->isDir()) {
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($this->tempDir) + 1);
// 添加 mimetype 文件时,不进行压缩
if ($relativePath === 'mimetype') {
$zip->addFile($filePath, $relativePath, ZipArchive::FL_NOCASE);
} else {
$zip->addFile($filePath, $relativePath);
}
}
}
$zip->close();
} else {
throw new Exception('无法创建 ZIP 文件');
}
}
protected function cleanup()
{
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($this->tempDir, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($files as $fileinfo) {
$todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
$todo($fileinfo->getRealPath());
}
rmdir($this->tempDir);
}
}
// 获取命令行参数
$filePath = $argv[1] ?? null;
$strategy = $argv[2] ?? 's2t'; // 默认策略为简体到繁体
$outputPath = $argv[3] ?? null;
// 检查文件路径
if (!$filePath || !file_exists($filePath)) {
echo "请提供有效的 epub 文件路径。\n";
exit(1);
}
// 执行转换
try {
$converter = new EPubConverter($filePath, $strategy, $outputPath);
$converter->convert();
echo "转换完成。\n";
} catch (Exception $e) {
echo "转换失败: " . $e->getMessage() . "\n";
}