class

zlib.ZipBuffer

class ZipBuffer

An in-memory, zero-copy view over the entries of a ZIP archive already held in a Buffer, TypedArray, DataView, or ArrayBuffer. Its set of entries can be edited - entries added or removed - but, unlike ZipFile, those edits are not written into the source buffer: a newly added entry is held as a separate in-memory ZipEntry (the passed buffer is a fixed-size view with no room to append to), and removal just drops the entry from ZipBuffer's index. The original bytes are never modified. zipBuffer.toBuffer() serializes the current set of entries into a fresh archive.

ZipBuffer does not copy the archive you hand it. It keeps a view onto that memory and reads each entry's content lazily and directly from it, which is what makes construction cheap regardless of archive size. The trade-off is that you must not modify or reuse that memory - including the ArrayBuffer backing a TypedArray/DataView - while the ZipBuffer, or any ZipEntry obtained from it, is still in use: a later read would observe the change and may fail or return corrupt data. Pass a copy (for example Buffer.from(source)) if the source might be mutated or reused.

add() and toBuffer() each have a *Sync counterpart (addSync(), toBufferSync()) that performs the same compression work synchronously. As with the synchronous node:fs APIs, these block the Node.js event loop and further JavaScript execution until the operation completes; use them only where synchronous execution is appropriate (for example, short-lived scripts or startup code), not in code that must stay responsive.

  • readonly comment: string

    The archive-level comment, preserved byte-for-byte across zipBuffer.toBuffer() calls unless overridden. The bytes are decoded as UTF-8 when they are valid UTF-8 and as CP437 otherwise (the field carries no encoding flag of its own).

  • readonly size: number

    The number of entries in the archive.

  • readonly writable: true

    Always true.

  • [Symbol.iterator](): Iterator<[name: string, entry: ZipEntry]>;
  • filename: string,
    ): Promise<ZipEntry>;

    Equivalent to zipBuffer.addEntry(await zlib.ZipEntry.create(filename, data, options)).

    @param filename

    The entry's name within the archive. A trailing / marks a directory entry.

    @param data

    The entry's complete, uncompressed content.

    @param options

    See zlib.ZipEntry.create().

    @returns

    Promise fulfilled with the created ZipEntry.

  • entry: ZipEntry

    Adds an already-built entry, keyed by its own zipEntry.name. Replaces any existing entry of that name.

    @returns

    entry

  • filename: string,

    The synchronous version of zipBuffer.add(). Equivalent to zipBuffer.addEntry(zlib.ZipEntry.createSync(filename, data, options)).

    @param filename

    The entry's name within the archive. A trailing / marks a directory entry.

    @param data

    The entry's complete, uncompressed content.

    @param options

    See zlib.ZipEntry.createSync().

    @returns

    The created entry.

  • clear(): void;

    Removes every entry.

  • name: string
    ): boolean;
    @returns

    true if an entry named name existed and was removed.

  • entries(): Iterator<[name: string, entry: ZipEntry]>;
    @returns

    Iterator of [name, entry] pairs, where entry is a ZipEntry.

  • callback: (entry: ZipEntry, name: string, zipBuffer: this) => void,
    thisArg?: any
    ): void;

    Calls callback once for each entry, in the order the archive lists them.

  • name: string

    Throws ERR_ZIP_ENTRY_NOT_FOUND if the archive has no entry named name.

  • name: string
    ): boolean;
  • keys(): Iterator<string>;
    @returns

    Iterator of entry names.

  • options?: string | CreateZipArchiveOptions
    ): Promise<NonSharedBuffer>;

    Serializes the current set of entries - in the order they were added or read - into a fresh archive, switching to Zip64 structures automatically as needed (see zlib.createZipArchive()).

    @param options

    An archive comment, as a shorthand for { comment: options }. The comment defaults to zipBuffer.comment.

    @returns

    Promise fulfilled with a Buffer containing the serialized archive.

  • options?: string | CreateZipArchiveOptions
    ): NonSharedBuffer;

    The synchronous version of zipBuffer.toBuffer() (see zlib.createZipArchiveSync()).

    @param options

    See zipBuffer.toBuffer().

    @returns

    The serialized archive.

  • values(): Iterator<ZipEntry>;
    @returns

    Iterator of ZipEntry.