Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(standalone/export): GBK encoding not detected
Elian Doranf3cfe2f2d38a
diff --git a/apps/client-standalone/src/lightweight/zip_provider.ts b/apps/client-standalone/src/lightweight/zip_provider.tsindex b0826b3a72..d9fb523ffe 100644--- a/apps/client-standalone/src/lightweight/zip_provider.ts+++ b/apps/client-standalone/src/lightweight/zip_provider.ts@@ -46,9 +46,14 @@ class BrowserZipArchive implements ZipArchive {}export default class BrowserZipProvider implements ZipProvider {- async detectFilenameEncoding(_buffer: Uint8Array): Promise<string> {- // fflate handles filename decoding internally; no additional detection needed.- return "utf-8";+ async detectFilenameEncoding(buffer: Uint8Array): Promise<string> {+ // fflate decodes filenames as CP437/Latin-1 (preserving raw bytes).+ // We recover raw bytes and detect the encoding.+ const rawSamples = await this.#collectRawFilenameSamples(buffer);+ if (rawSamples.length === 0) {+ return "utf-8";+ }+ return detectEncodingFromBytes(rawSamples);}createZipArchive(): ZipArchive {@@ -62,7 +67,7 @@ export default class BrowserZipProvider implements ZipProvider {readZipFile(buffer: Uint8Array,processEntry: (entry: ZipEntry, readContent: () => Promise<Uint8Array>) => Promise<void>,- _filenameEncoding?: string+ filenameEncoding?: string): Promise<void> {return new Promise<void>((res, rej) => {unzip(buffer, async (err, files) => {@@ -71,7 +76,7 @@ export default class BrowserZipProvider implements ZipProvider {try {for (const [fileName, data] of Object.entries(files)) {await processEntry(- { fileName: decodeZipFileName(fileName) },+ { fileName: decodeZipFileName(fileName, filenameEncoding) },() => Promise.resolve(data));}@@ -82,26 +87,92 @@ export default class BrowserZipProvider implements ZipProvider {});});}++ /**+ * Does a first pass over the ZIP to collect raw filename bytes+ * from entries that aren't valid UTF-8.+ */+ #collectRawFilenameSamples(buffer: Uint8Array): Promise<Uint8Array[]> {+ return new Promise<Uint8Array[]>((res, rej) => {+ unzip(buffer, (err, files) => {+ if (err) { rej(err); return; }++ const samples: Uint8Array[] = [];+ const utf8 = new TextDecoder("utf-8", { fatal: true });+ for (const fileName of Object.keys(files)) {+ const bytes = recoverRawBytes(fileName);+ try {+ utf8.decode(bytes);+ } catch {+ samples.push(bytes);+ }+ }+ res(samples);+ });+ });+ }}-const utf8Decoder = new TextDecoder("utf-8", { fatal: true });+/** Recover original raw bytes from fflate's CP437/Latin-1 decoded string. */+function recoverRawBytes(name: string): Uint8Array {+ const bytes = new Uint8Array(name.length);+ for (let i = 0; i < name.length; i++) {+ bytes[i] = name.charCodeAt(i) & 0xff;+ }+ return bytes;+}/*** fflate decodes ZIP entry filenames as CP437/Latin-1 unless the language* encoding flag (general purpose bit 11) is set, but many real-world archives- * (e.g. those produced by macOS / Linux unzip / Python's zipfile) write UTF-8- * filenames without setting that flag. Recover the original UTF-8 bytes from- * fflate's per-byte string and re-decode them; if the result isn't valid- * UTF-8 we fall back to the as-decoded name.+ * write UTF-8 or other encodings without setting that flag.+ * Recover the original raw bytes and re-decode with the detected encoding.*/-function decodeZipFileName(name: string): string {- const bytes = new Uint8Array(name.length);- for (let i = 0; i < name.length; i++) {- bytes[i] = name.charCodeAt(i) & 0xff;- }+function decodeZipFileName(name: string, encoding?: string): string {+ const bytes = recoverRawBytes(name);try {- return utf8Decoder.decode(bytes);+ return new TextDecoder(encoding || "utf-8", { fatal: true }).decode(bytes);} catch {+ if (encoding && encoding !== "utf-8") {+ // Encoding detection was wrong for this entry, try UTF-8+ try {+ return new TextDecoder("utf-8", { fatal: true }).decode(bytes);+ } catch {+ return name;+ }+ }return name;}}++/** Common CJK encodings to try when filenames aren't valid UTF-8. */+const CANDIDATE_ENCODINGS = ["gbk", "shift_jis", "euc-kr", "big5"];++/**+ * Detect encoding from raw filename bytes by trying TextDecoder with+ * common encodings. Returns the first encoding that can decode all samples+ * without errors, or "utf-8" as fallback.+ */+function detectEncodingFromBytes(samples: Uint8Array[]): string {+ for (const encoding of CANDIDATE_ENCODINGS) {+ try {+ const decoder = new TextDecoder(encoding, { fatal: true });+ let valid = true;+ for (const sample of samples) {+ try {+ decoder.decode(sample);+ } catch {+ valid = false;+ break;+ }+ }+ if (valid) {+ return encoding;+ }+ } catch {+ // TextDecoder doesn't support this encoding in this environment+ continue;+ }+ }+ return "utf-8";+}