mirror of
https://github.com/nodejs/node.git
synced 2026-11-21 12:59:16 -05:00
Add ZIP archive support to the node:zlib module through three classes and a set of helpers: - ZipEntry: a single archive member, with buffered reads (content()), bounded-memory streaming reads (contentIterator()), and create()/createStream() for building members. - ZipFile: random access to an archive backed by a file descriptor, reading members lazily without retaining their content and writing new members in place; opened with open()/openSync(). - ZipBuffer: a zero-copy, in-memory view over an archive already held in a Buffer. createZipArchive() serializes a sequence of entries into an archive byte stream, and setMaxZipContentSize() bounds the default in-memory decompression size. Every operation has both an asynchronous and a synchronous form. Signed-off-by: Philipp Dunkel <pip@pipobscure.com> PR-URL: https://github.com/nodejs/node/pull/64339 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Aviv Keller <me@aviv.sh>
39 lines
1.3 KiB
JavaScript
39 lines
1.3 KiB
JavaScript
'use strict';
|
|
|
|
require('../common');
|
|
|
|
const assert = require('node:assert');
|
|
const zlib = require('node:zlib');
|
|
const { test } = require('node:test');
|
|
|
|
const ZIP64_EOCD_SIGNATURE = Buffer.from([0x50, 0x4b, 0x06, 0x06]);
|
|
|
|
async function buildArchive(count) {
|
|
const entries = [];
|
|
for (let i = 0; i < count; i++) {
|
|
entries.push(await zlib.ZipEntry.create(`entry-${i}`, Buffer.alloc(0), { method: 'store' }));
|
|
}
|
|
const chunks = [];
|
|
for await (const chunk of zlib.createZipArchive(entries)) chunks.push(chunk);
|
|
return Buffer.concat(chunks);
|
|
}
|
|
|
|
test('an entry count at or above 0xFFFF forces Zip64 structures', async () => {
|
|
const archive = await buildArchive(0x10000);
|
|
assert.ok(archive.includes(ZIP64_EOCD_SIGNATURE));
|
|
|
|
const read = [...zlib.ZipEntry.read(archive)];
|
|
assert.strictEqual(read.length, 0x10000);
|
|
|
|
using zip = new zlib.ZipBuffer(archive);
|
|
assert.strictEqual(zip.size, 0x10000);
|
|
assert.strictEqual(zip.has('entry-0'), true);
|
|
assert.strictEqual(zip.has(`entry-${0x10000 - 1}`), true);
|
|
}, { timeout: 120_000 });
|
|
|
|
test('an archive below the Zip64 thresholds contains no Zip64 structures', async () => {
|
|
const archive = await buildArchive(10);
|
|
assert.ok(!archive.includes(ZIP64_EOCD_SIGNATURE));
|
|
assert.strictEqual([...zlib.ZipEntry.read(archive)].length, 10);
|
|
});
|