Skip to content

Commit a892e34

Browse files
committed
Fixed conversion for certain EPUBs with improperly formatted nav files
1 parent 0f1fe29 commit a892e34

3 files changed

Lines changed: 18 additions & 19 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## v2026.04.22-1
2+
- Fixed conversion for certain EPUBs with improperly formatted nav files
3+
14
## v2026.04.15-1
25
- Fixed chapter support for KOReader via folders
36

@@ -11,7 +14,7 @@
1114
- Increased duplicate Cover threshold from 95 to 97.5%
1215

1316
## v2025.11.15-2
14-
- Fixed epubs that only contain images
17+
- Fixed EPUBs that only contain images
1518

1619
## v2025.11.15-1
1720
- Fixed chapter detection in more rare cases

‎source/Program.cs‎

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static class VersionDate
2727
{
2828
public static string GetVersionDateYear { get; } = "2026";
2929
public static string GetVersionDateMonth { get; } = "04";
30-
public static string GetVersionDateDay { get; } = "15";
30+
public static string GetVersionDateDay { get; } = "22";
3131
public static int GetVersionNumber { get; } = 1;
3232
}
3333

@@ -565,9 +565,8 @@ private static bool CheckDRMProtection(Dictionary<string, ZipArchiveEntry> entry
565565
filename.EndsWith(".html", StringComparison.InvariantCultureIgnoreCase) ||
566566
filename.EndsWith(".xml", StringComparison.InvariantCultureIgnoreCase)) // Walter Isaacson - Steve Jobs
567567
{
568-
using StreamReader reader = new(fileEntry.Open());
568+
using StreamReader reader = new(fileEntry.Open(), Encoding.UTF8, detectEncodingFromByteOrderMarks: true);
569569
string fileContent = reader.ReadToEnd();
570-
fileContent = Encoding.UTF8.GetString(reader.CurrentEncoding.GetBytes(fileContent));
571570
if (fileContent.Contains("html", StringComparison.InvariantCultureIgnoreCase)) isDRMProtected = false;
572571
}
573572
else if (imageExtensions.Any(ext => filename.EndsWith(ext, StringComparison.InvariantCultureIgnoreCase)))
@@ -1113,9 +1112,8 @@ private static (XDocument, string) GetBarnesAndNobleReplicaMap(Dictionary<string
11131112
opfReplicaMap = ResolveRootPath(opfPath, opfReplicaMap);
11141113

11151114
ZipArchiveEntry fileEntry = entryMap.GetValueOrDefault(opfReplicaMap)!;
1116-
using StreamReader reader = new(fileEntry.Open());
1115+
using StreamReader reader = new(fileEntry.Open(), Encoding.UTF8, detectEncodingFromByteOrderMarks: true);
11171116
string opfContent = reader.ReadToEnd();
1118-
opfContent = Encoding.UTF8.GetString(reader.CurrentEncoding.GetBytes(opfContent));
11191117

11201118
XDocument replicaMapDoc = XDocument.Parse(opfContent);
11211119

@@ -1126,9 +1124,8 @@ private static XDocument GetOpfDocument(Dictionary<string, ZipArchiveEntry> entr
11261124
string opfPath)
11271125
{
11281126
ZipArchiveEntry fileEntry = entryMap.GetValueOrDefault(opfPath)!;
1129-
using StreamReader reader = new(fileEntry.Open());
1127+
using StreamReader reader = new(fileEntry.Open(), Encoding.UTF8, detectEncodingFromByteOrderMarks: true);
11301128
string opfContent = reader.ReadToEnd();
1131-
opfContent = Encoding.UTF8.GetString(reader.CurrentEncoding.GetBytes(opfContent));
11321129

11331130
// Replace null characters
11341131
opfContent = opfContent.Replace("\0", string.Empty).Replace("\x01", string.Empty);
@@ -1463,9 +1460,8 @@ private static List<Dictionary<string, string>> GetTocFile(Dictionary<string, Zi
14631460
{
14641461

14651462
}
1466-
using StreamReader reader = new(altTocEntry!.Open());
1463+
using StreamReader reader = new(altTocEntry!.Open(), Encoding.UTF8, detectEncodingFromByteOrderMarks: true);
14671464
string altTocContent = reader.ReadToEnd();
1468-
altTocContent = Encoding.UTF8.GetString(reader.CurrentEncoding.GetBytes(altTocContent));
14691465

14701466
XDocument altTocDoc = XDocument.Parse(altTocContent);
14711467
XNamespace altToc = "http://www.w3.org/1999/xhtml";
@@ -2183,9 +2179,12 @@ private static List<Dictionary<string, string>> ParseEpubToc(Dictionary<string,
21832179
foreach (string navPath in navPaths)
21842180
{
21852181
ZipArchiveEntry tocEntry = entryMap.GetValueOrDefault(navPath)!;
2186-
using StreamReader reader = new(tocEntry.Open());
2182+
using StreamReader reader = new(tocEntry.Open(), Encoding.UTF8, detectEncodingFromByteOrderMarks: true);
21872183
string tocContent = reader.ReadToEnd();
2188-
tocContent = Encoding.UTF8.GetString(reader.CurrentEncoding.GetBytes(tocContent));
2184+
2185+
#pragma warning disable SYSLIB1045 // "GeneratedRegexAttribute"
2186+
tocContent = Regex.Replace(tocContent, @"&(?!([a-zA-Z]+|#\d+|#x[a-zA-Z0-9]+);)", "&amp;");
2187+
#pragma warning restore SYSLIB1045
21892188

21902189
XDocument tocDoc = XDocument.Parse(tocContent);
21912190
XNamespace ops = "http://www.idpf.org/2007/ops";
@@ -2284,9 +2283,8 @@ private static string FindImagePathInCss(Dictionary<string, ZipArchiveEntry> ent
22842283
return imagePath;
22852284
}
22862285

2287-
using StreamReader reader = new(fileEntry.Open());
2286+
using StreamReader reader = new(fileEntry.Open(), Encoding.UTF8, detectEncodingFromByteOrderMarks: true);
22882287
string fileContent = reader.ReadToEnd();
2289-
fileContent = Encoding.UTF8.GetString(reader.CurrentEncoding.GetBytes(fileContent));
22902288

22912289
var parser = new StylesheetParser();
22922290
var stylesheet = parser.Parse(fileContent);
@@ -2329,9 +2327,8 @@ private static string FindImagePathInCss(Dictionary<string, ZipArchiveEntry> ent
23292327
actualFilename.EndsWith(".html", StringComparison.InvariantCultureIgnoreCase) ||
23302328
actualFilename.EndsWith(".xml", StringComparison.InvariantCultureIgnoreCase))
23312329
{
2332-
using StreamReader reader = new(fileEntry.Open());
2330+
using StreamReader reader = new(fileEntry.Open(), Encoding.UTF8, detectEncodingFromByteOrderMarks: true);
23332331
string fileContent = reader.ReadToEnd();
2334-
fileContent = Encoding.UTF8.GetString(reader.CurrentEncoding.GetBytes(fileContent));
23352332

23362333
#if DEBUG
23372334
/// Likely the last page of samples
@@ -2698,9 +2695,8 @@ private static string GetOpfFile(Dictionary<string, ZipArchiveEntry> entryMap)
26982695
const string containerPath = "META-INF/container.xml";
26992696

27002697
ZipArchiveEntry xmlEntry = entryMap.GetValueOrDefault(containerPath) ?? throw new Exception(Resources.ContainerXMLNotFound);
2701-
using StreamReader reader = new(xmlEntry.Open());
2698+
using StreamReader reader = new(xmlEntry.Open(), Encoding.UTF8, detectEncodingFromByteOrderMarks: true);
27022699
string xmlContent = reader.ReadToEnd();
2703-
xmlContent = Encoding.UTF8.GetString(reader.CurrentEncoding.GetBytes(xmlContent));
27042700

27052701
XDocument xmlDoc = XDocument.Parse(xmlContent);
27062702
XNamespace xmlns = "urn:oasis:names:tc:opendocument:xmlns:container";

‎ver/latest‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v2026.04.15-1
1+
v2026.04.22-1

0 commit comments

Comments
 (0)