Open a Character.AI data export (ZIP or JSON) locally and save chats as .txt. Pure parser ported from kamiko.fun's hosted viewer, including the nested turn_key.chat_id split; minimal dependency-free ZIP reader using DecompressionStream in the browser and zlib in Node; synthetic sample fixture; MIT. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
116 lines
4.5 KiB
JavaScript
116 lines
4.5 KiB
JavaScript
/**
|
|
* zip.js — a minimal, dependency-free ZIP reader for the export viewer.
|
|
*
|
|
* Character.AI sends the export as a ZIP of JSON files. This reads the central directory and hands
|
|
* back each entry's raw bytes plus its compression method. Decompression is injected by the caller
|
|
* because the two runtimes differ:
|
|
* - browser: `new DecompressionStream('deflate-raw')` (index.html)
|
|
* - Node: `zlib.inflateRawSync` (cli.js)
|
|
*
|
|
* Supports: stored (method 0) and deflate (method 8). Not supported: ZIP64, encryption, other
|
|
* methods. An entry it cannot open is reported, not thrown on.
|
|
*
|
|
* Plain script: exposes `window.CaiZip` in a browser, `module.exports` in Node.
|
|
*/
|
|
(function (root, factory) {
|
|
const api = factory();
|
|
if (typeof module === 'object' && module.exports) module.exports = api;
|
|
root.CaiZip = api;
|
|
})(typeof globalThis !== 'undefined' ? globalThis : this, function () {
|
|
'use strict';
|
|
|
|
const SIG_EOCD = 0x06054b50;
|
|
const SIG_CENTRAL = 0x02014b50;
|
|
const SIG_LOCAL = 0x04034b50;
|
|
const METHOD_STORED = 0;
|
|
const METHOD_DEFLATE = 8;
|
|
/** Longest ZIP comment the spec allows, so the EOCD scan stays bounded. */
|
|
const MAX_COMMENT = 0xffff;
|
|
|
|
const utf8 = new TextDecoder('utf-8');
|
|
|
|
function findEocd(view) {
|
|
const stop = Math.max(0, view.byteLength - 22 - MAX_COMMENT);
|
|
for (let i = view.byteLength - 22; i >= stop; i -= 1) {
|
|
if (view.getUint32(i, true) === SIG_EOCD) return i;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
/**
|
|
* List every entry in a ZIP.
|
|
* @param {Uint8Array} bytes
|
|
* @returns {{ entries: Array<{ name: string, method: number, data: Uint8Array, size: number }>, error: string }}
|
|
*/
|
|
function listEntries(bytes) {
|
|
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
const eocd = findEocd(view);
|
|
if (eocd < 0) return { entries: [], error: 'not_a_zip' };
|
|
|
|
const count = view.getUint16(eocd + 10, true);
|
|
let offset = view.getUint32(eocd + 16, true);
|
|
const entries = [];
|
|
|
|
for (let n = 0; n < count; n += 1) {
|
|
if (offset + 46 > view.byteLength || view.getUint32(offset, true) !== SIG_CENTRAL) {
|
|
return { entries, error: 'central_directory_damaged' };
|
|
}
|
|
const method = view.getUint16(offset + 10, true);
|
|
const compressedSize = view.getUint32(offset + 20, true);
|
|
const size = view.getUint32(offset + 24, true);
|
|
const nameLength = view.getUint16(offset + 28, true);
|
|
const extraLength = view.getUint16(offset + 30, true);
|
|
const commentLength = view.getUint16(offset + 32, true);
|
|
const localOffset = view.getUint32(offset + 42, true);
|
|
const name = utf8.decode(bytes.subarray(offset + 46, offset + 46 + nameLength));
|
|
|
|
if (localOffset + 30 > view.byteLength || view.getUint32(localOffset, true) !== SIG_LOCAL) {
|
|
return { entries, error: 'local_header_damaged' };
|
|
}
|
|
const localNameLength = view.getUint16(localOffset + 26, true);
|
|
const localExtraLength = view.getUint16(localOffset + 28, true);
|
|
const dataStart = localOffset + 30 + localNameLength + localExtraLength;
|
|
const data = bytes.subarray(dataStart, dataStart + compressedSize);
|
|
|
|
entries.push({ name, method, data, size });
|
|
offset += 46 + nameLength + extraLength + commentLength;
|
|
}
|
|
return { entries, error: '' };
|
|
}
|
|
|
|
/** True for entries the viewer wants: JSON files, not macOS resource-fork copies, not folders. */
|
|
function isWantedEntry(entry) {
|
|
const lower = entry.name.toLowerCase();
|
|
return lower.endsWith('.json') && !entry.name.startsWith('__MACOSX/') && !entry.name.endsWith('/');
|
|
}
|
|
|
|
/**
|
|
* Open every JSON entry of a ZIP as { name, text }.
|
|
* @param {Uint8Array} bytes
|
|
* @param {(data: Uint8Array) => Promise<Uint8Array> | Uint8Array} inflateRaw
|
|
* @returns {Promise<{ files: Array<{ name: string, text: string }>, skipped: string[], error: string }>}
|
|
*/
|
|
async function readJsonEntries(bytes, inflateRaw) {
|
|
const { entries, error } = listEntries(bytes);
|
|
const files = [];
|
|
const skipped = [];
|
|
for (const entry of entries) {
|
|
if (!isWantedEntry(entry)) continue;
|
|
try {
|
|
if (entry.method === METHOD_STORED) {
|
|
files.push({ name: entry.name, text: utf8.decode(entry.data) });
|
|
} else if (entry.method === METHOD_DEFLATE) {
|
|
files.push({ name: entry.name, text: utf8.decode(await inflateRaw(entry.data)) });
|
|
} else {
|
|
skipped.push(entry.name);
|
|
}
|
|
} catch {
|
|
skipped.push(entry.name);
|
|
}
|
|
}
|
|
return { files, skipped, error };
|
|
}
|
|
|
|
return { listEntries, readJsonEntries, METHOD_STORED, METHOD_DEFLATE };
|
|
});
|