archiver + unzipper zip round trip in memory: working versions (tested 2026-10-15)
Stream text and binary entries into an in-memory zip with archiver, then read it back with unzipper via central-directory random access and a streaming entry parser.
passed4 passed · 0 failed2026-10-15 20:08 UTC- Status
- passed
- Last passed
- 2026-10-15 20:08 UTC
- Tests
- 4 / 4
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions4
archiver@8.0.0unzipper@0.12.5@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38799498972
The code
Exactly the files that passed. Your agent can pull them with get_recipe("archiver-unzipper-zip-roundtrip") (install), or see them on GitHub.
package.json
{
"name": "archiver-unzipper-zip-roundtrip",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"archiver": "8.0.0",
"unzipper": "0.12.5"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15"
}
}
src/zip.ts
import { ZipArchive } from "archiver";
import unzipper from "unzipper";
import { PassThrough, Readable } from "node:stream";
export type Entry = { name: string; content: string | Buffer };
/** Stream entries into a zip held in memory (archiver 8 is ESM and exports ZipArchive instead of the old archiver("zip") factory). */
export async function zipToBuffer(entries: Entry[], level = 9): Promise<Buffer> {
const archive = new ZipArchive({ zlib: { level } });
const sink = new PassThrough();
const chunks: Buffer[] = [];
sink.on("data", (c: Buffer) => chunks.push(c));
const done = new Promise<void>((res, rej) => { sink.on("end", res); archive.on("error", rej); });
archive.pipe(sink);
for (const e of entries) {
if (typeof e.content === "string") archive.append(e.content, { name: e.name, date: new Date("2026-01-01T00:00:00Z") });
else archive.append(Readable.from([e.content]), { name: e.name, date: new Date("2026-01-01T00:00:00Z") });
}
await archive.finalize();
await done;
return Buffer.concat(chunks);
}
/** Random-access read of the central directory. */
export async function listZip(buf: Buffer) {
const dir = await unzipper.Open.buffer(buf);
return dir.files.filter((f) => f.type === "File").map((f) => ({ path: f.path, size: f.uncompressedSize }));
}
export async function readEntry(buf: Buffer, path: string) {
const dir = await unzipper.Open.buffer(buf);
const f = dir.files.find((x) => x.path === path);
return f ? (await f.buffer()) : null;
}
/** Streaming parse: walk entries in order without loading the whole archive. */
export async function streamEntries(buf: Buffer) {
const out: Record<string, string> = {};
const parser = Readable.from([buf]).pipe(unzipper.Parse({ forceStream: true }));
for await (const entry of parser as AsyncIterable<unzipper.Entry>) {
out[entry.path] = (await entry.buffer()).toString("utf8");
}
return out;
}
test/zip.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { zipToBuffer, listZip, readEntry, streamEntries } from "../src/zip.ts";
const big = "x".repeat(100_000);
const bin = Buffer.from([0, 1, 2, 250, 251, 252]);
test("zip in memory and list entries", async () => {
const buf = await zipToBuffer([
{ name: "readme.txt", content: "hello" },
{ name: "data/big.txt", content: big },
{ name: "data/blob.bin", content: bin },
]);
assert.equal(buf.subarray(0, 2).toString(), "PK");
assert.ok(buf.length < 2000, "deflate should shrink repetitive data");
assert.deepEqual(await listZip(buf), [
{ path: "readme.txt", size: 5 },
{ path: "data/big.txt", size: 100_000 },
{ path: "data/blob.bin", size: 6 },
]);
});
test("random access read of one entry, binary safe", async () => {
const buf = await zipToBuffer([{ name: "a.txt", content: "A" }, { name: "blob.bin", content: bin }]);
assert.deepEqual(await readEntry(buf, "blob.bin"), bin);
assert.equal(await readEntry(buf, "missing"), null);
});
test("streaming parse round-trips text", async () => {
const buf = await zipToBuffer([{ name: "one.txt", content: "1" }, { name: "dir/two.txt", content: "two ✓" }]);
assert.deepEqual(await streamEntries(buf), { "one.txt": "1", "dir/two.txt": "two ✓" });
});
test("store mode (level 0) keeps size", async () => {
const buf = await zipToBuffer([{ name: "big.txt", content: big }], 0);
assert.ok(buf.length > 100_000);
assert.equal((await readEntry(buf, "big.txt"))?.toString(), big);
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.