MCPcopy Create free account
hub / github.com/MoonshotAI/kimi-code / readZipEntries

Function readZipEntries

packages/node-sdk/test/export-session.test.ts:41–82  ·  view source on GitHub ↗
(buf: Buffer)

Source from the content-addressed store, hash-verified

39}
40
41function readZipEntries(buf: Buffer): Map<string, Buffer> {
42 let eocd = -1;
43 for (let i = buf.length - 22; i >= Math.max(0, buf.length - 65557); i--) {
44 if (buf.readUInt32LE(i) === 0x06054b50) {
45 eocd = i;
46 break;
47 }
48 }
49 if (eocd === -1) throw new Error('zip eocd not found');
50
51 const entryCount = buf.readUInt16LE(eocd + 10);
52 const cdOffset = buf.readUInt32LE(eocd + 16);
53 const entries = new Map<string, Buffer>();
54 let pos = cdOffset;
55
56 for (let i = 0; i < entryCount; i++) {
57 if (buf.readUInt32LE(pos) !== 0x02014b50) {
58 throw new Error(`bad central-directory entry at ${String(pos)}`);
59 }
60 const method = buf.readUInt16LE(pos + 10);
61 const compressedSize = buf.readUInt32LE(pos + 20);
62 const fnameLen = buf.readUInt16LE(pos + 28);
63 const extraLen = buf.readUInt16LE(pos + 30);
64 const commentLen = buf.readUInt16LE(pos + 32);
65 const lfhOffset = buf.readUInt32LE(pos + 42);
66 const filename = buf.toString('utf8', pos + 46, pos + 46 + fnameLen);
67
68 if (buf.readUInt32LE(lfhOffset) !== 0x04034b50) {
69 throw new Error(`bad local-file-header at ${String(lfhOffset)}`);
70 }
71 const lfhFnameLen = buf.readUInt16LE(lfhOffset + 26);
72 const lfhExtraLen = buf.readUInt16LE(lfhOffset + 28);
73 const dataStart = lfhOffset + 30 + lfhFnameLen + lfhExtraLen;
74 const compressed = buf.subarray(dataStart, dataStart + compressedSize);
75 const data = method === 0 ? compressed : method === 8 ? zlib.inflateRawSync(compressed) : null;
76 if (data === null) throw new Error(`unsupported compression method: ${String(method)}`);
77 entries.set(filename, data);
78 pos += 46 + fnameLen + extraLen + commentLen;
79 }
80
81 return entries;
82}
83
84function makeSummary(input: {
85 readonly id: string;

Callers 1

Calls 2

toStringMethod · 0.65
setMethod · 0.65

Tested by

no test coverage detected