method

zlib.ZipEntry.createStream

static createStream(
filename: string,
source: AsyncIterable<Uint8Array<ArrayBufferLike>>,

Creates an entry whose content is compressed on the fly as it is serialized by zlib.createZipArchive(), without buffering source in memory. Its size, compressedSize, and crc32 only become available once serialization has finished. There is no synchronous counterpart: streaming entries only make sense with an asynchronous, incrementally-produced source.

source is drained exactly once, during serialization. Until that happens the entry has no readable content, so zipEntry.content(), zipEntry.contentSync(), and zipEntry.contentIterator() throw ERR_INVALID_STATE. If the entry is serialized by adding it to a writable ZipFile with zipFile.addEntry() (or addEntrySync()), it is then promoted in place to a file-backed entry pointing at the copy just written, so it becomes readable (and can be serialized again) for as long as that ZipFile stays open. Serializing it any other way (for example directly through zlib.createZipArchive()) leaves it spent and unreadable.

Because source may hold an operating-system resource (a file read stream, say), a streaming entry is disposable: its Symbol.dispose and Symbol.asyncDispose methods destroy source if it has not been consumed. An entry passed to an archive is disposed by that archive (see zlib.createZipArchive()); dispose an entry directly only when it was built but never handed to one. Disposal is a no-op for non-streaming entries - in particular a file-backed entry never closes the ZipFile descriptor it borrows.

@param filename

The entry's name within the archive. Must not end in /.

@param source

Yields the entry's uncompressed content as Uint8Array chunks.

Referenced types

class Uint8Array<TArrayBuffer extends ArrayBufferLike = ArrayBufferLike>

A typed array of 8-bit unsigned integer values. The contents are initialized to 0. If the requested number of bytes could not be allocated an exception is raised.

  • readonly [Symbol.toStringTag]: 'Uint8Array'
  • readonly buffer: TArrayBuffer

    The ArrayBuffer instance referenced by the array.

  • readonly byteLength: number

    The length in bytes of the array.

  • readonly byteOffset: number

    The offset in bytes of the array.

  • readonly BYTES_PER_ELEMENT: number

    The size in bytes of each element in the array.

  • readonly length: number

    The length of the array.

  • [Symbol.iterator](): ArrayIterator<number>;
  • index: number
    ): undefined | number;

    Returns the item located at the specified index.

    @param index

    The zero-based index of the desired code unit. A negative index will count back from the last item.

  • target: number,
    start: number,
    end?: number
    ): this;

    Returns the this object after copying a section of the array identified by start and end to the same array starting at position target

    @param target

    If target is negative, it is treated as length+target where length is the length of the array.

    @param start

    If start is negative, it is treated as length+start. If end is negative, it is treated as length+end.

    @param end

    If not specified, length of the this object is used as its default value.

  • entries(): ArrayIterator<[number, number]>;

    Returns an array of key, value pairs for every entry in the array

  • predicate: (value: number, index: number, array: this) => unknown,
    thisArg?: any
    ): boolean;

    Determines whether all the members of an array satisfy the specified test.

    @param predicate

    A function that accepts up to three arguments. The every method calls the predicate function for each element in the array until the predicate returns a value which is coercible to the Boolean value false, or until the end of the array.

    @param thisArg

    An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

  • value: number,
    start?: number,
    end?: number
    ): this;

    Changes all array elements from start to end index to a static value and returns the modified array

    @param value

    value to fill array section with

    @param start

    index to start filling the array at. If start is negative, it is treated as length+start where length is the length of the array.

    @param end

    index to stop filling the array at. If end is negative, it is treated as length+end.

  • predicate: (value: number, index: number, array: this) => any,
    thisArg?: any

    Returns the elements of an array that meet the condition specified in a callback function.

    @param predicate

    A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.

    @param thisArg

    An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

  • predicate: (value: number, index: number, obj: this) => boolean,
    thisArg?: any
    ): undefined | number;

    Returns the value of the first element in the array where predicate is true, and undefined otherwise.

    @param predicate

    find calls predicate once for each element of the array, in ascending order, until it finds one where predicate returns true. If such an element is found, find immediately returns that element value. Otherwise, find returns undefined.

    @param thisArg

    If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

  • predicate: (value: number, index: number, obj: this) => boolean,
    thisArg?: any
    ): number;

    Returns the index of the first element in the array where predicate is true, and -1 otherwise.

    @param predicate

    find calls predicate once for each element of the array, in ascending order, until it finds one where predicate returns true. If such an element is found, findIndex immediately returns that element index. Otherwise, findIndex returns -1.

    @param thisArg

    If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

  • findLast<S extends number>(
    predicate: (value: number, index: number, array: this) => value is S,
    thisArg?: any
    ): undefined | S;

    Returns the value of the last element in the array where predicate is true, and undefined otherwise.

    @param predicate

    findLast calls predicate once for each element of the array, in descending order, until it finds one where predicate returns true. If such an element is found, findLast immediately returns that element value. Otherwise, findLast returns undefined.

    @param thisArg

    If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

    predicate: (value: number, index: number, array: this) => unknown,
    thisArg?: any
    ): undefined | number;
  • predicate: (value: number, index: number, array: this) => unknown,
    thisArg?: any
    ): number;

    Returns the index of the last element in the array where predicate is true, and -1 otherwise.

    @param predicate

    findLastIndex calls predicate once for each element of the array, in descending order, until it finds one where predicate returns true. If such an element is found, findLastIndex immediately returns that element index. Otherwise, findLastIndex returns -1.

    @param thisArg

    If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

  • callbackfn: (value: number, index: number, array: this) => void,
    thisArg?: any
    ): void;

    Performs the specified action for each element in an array.

    @param callbackfn

    A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.

    @param thisArg

    An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

  • searchElement: number,
    fromIndex?: number
    ): boolean;

    Determines whether an array includes a certain element, returning true or false as appropriate.

    @param searchElement

    The element to search for.

    @param fromIndex

    The position in this array at which to begin searching for searchElement.

  • searchElement: number,
    fromIndex?: number
    ): number;

    Returns the index of the first occurrence of a value in an array.

    @param searchElement

    The value to locate in the array.

    @param fromIndex

    The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.

  • separator?: string
    ): string;

    Adds all the elements of an array separated by the specified separator string.

    @param separator

    A string used to separate one element of an array from the next in the resulting String. If omitted, the array elements are separated with a comma.

  • keys(): ArrayIterator<number>;

    Returns an list of keys in the array

  • searchElement: number,
    fromIndex?: number
    ): number;

    Returns the index of the last occurrence of a value in an array.

    @param searchElement

    The value to locate in the array.

    @param fromIndex

    The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.

  • callbackfn: (value: number, index: number, array: this) => number,
    thisArg?: any

    Calls a defined callback function on each element of an array, and returns an array that contains the results.

    @param callbackfn

    A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.

    @param thisArg

    An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

  • callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: this) => number
    ): number;

    Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

    @param callbackfn

    A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.

    callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: this) => number,
    initialValue: number
    ): number;
    reduce<U>(
    callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: this) => U,
    initialValue: U
    ): U;

    Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

    @param callbackfn

    A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.

    @param initialValue

    If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.

  • callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: this) => number
    ): number;

    Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

    @param callbackfn

    A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.

    callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: this) => number,
    initialValue: number
    ): number;
    callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: this) => U,
    initialValue: U
    ): U;

    Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

    @param callbackfn

    A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.

    @param initialValue

    If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.

  • reverse(): this;

    Reverses the elements in an Array.

  • array: ArrayLike<number>,
    offset?: number
    ): void;

    Sets a value or an array of values.

    @param array

    A typed or untyped array of values to set.

    @param offset

    The index in the current array at which the values are to be written.

  • base64: string,
    offset?: number
    ): { read: number; written: number };

    Set the contents of the Uint8Array from a base64 encoded string

    @param base64

    The base64 encoded string to decode into the array

    @param offset

    Optional starting index to begin setting the decoded bytes (default: 0)

  • hex: string
    ): { read: number; written: number };

    Set the contents of the Uint8Array from a hex encoded string

    @param hex

    The hex encoded string to decode into the array. The string must have an even number of characters, contain only hexadecimal characters, and contain no whitespace.

  • start?: number,
    end?: number

    Returns a section of an array.

    @param start

    The beginning of the specified portion of the array.

    @param end

    The end of the specified portion of the array. This is exclusive of the element at the index 'end'.

  • predicate: (value: number, index: number, array: this) => unknown,
    thisArg?: any
    ): boolean;

    Determines whether the specified callback function returns true for any element of an array.

    @param predicate

    A function that accepts up to three arguments. The some method calls the predicate function for each element in the array until the predicate returns a value which is coercible to the Boolean value true, or until the end of the array.

    @param thisArg

    An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

  • compareFn?: (a: number, b: number) => number
    ): this;

    Sorts an array.

    @param compareFn

    Function used to determine the order of the elements. It is expected to return a negative value if first argument is less than second argument, zero if they're equal and a positive value otherwise. If omitted, the elements are sorted in ascending order.

    [11,2,22,1].sort((a, b) => a - b)
  • begin?: number,
    end?: number
    ): Uint8Array<TArrayBuffer>;

    Gets a new Uint8Array view of the ArrayBuffer store for this array, referencing the elements at begin, inclusive, up to end, exclusive.

    @param begin

    The index of the beginning of the array.

    @param end

    The index of the end of the array.

  • options?: { alphabet: 'base64' | 'base64url'; omitPadding: boolean }
    ): string;

    Convert the Uint8Array to a base64 encoded string

  • toHex(): string;

    Convert the Uint8Array to a hex encoded string

  • toLocaleString(): string;

    Converts a number to a string by using the current locale.

    locales: string | string[],
    options?: NumberFormatOptions
    ): string;
  • Copies the array and returns the copy with the elements in reverse order.

  • compareFn?: (a: number, b: number) => number

    Copies and sorts the array.

    @param compareFn

    Function used to determine the order of the elements. It is expected to return a negative value if the first argument is less than the second argument, zero if they're equal, and a positive value otherwise. If omitted, the elements are sorted in ascending order.

    const myNums = Uint8Array.from([11, 2, 22, 1]);
    myNums.toSorted((a, b) => a - b) // Uint8Array(4) [1, 2, 11, 22]
  • toString(): string;

    Returns a string representation of an array.

  • valueOf(): this;

    Returns the primitive value of the specified object.

  • values(): ArrayIterator<number>;

    Returns an list of values in the array

  • index: number,
    value: number

    Copies the array and inserts the given number at the provided index.

    @param index

    The index of the value to overwrite. If the index is negative, then it replaces from the end of the array.

    @param value

    The value to insert into the copied array.

    @returns

    A copy of the original array with the inserted value.

interface ZipEntryCreateStreamOptions

  • comment?: string

    An entry comment.

  • method?: 'zstd' | 'deflate' | 'store'

    Compression method.

  • mode?: number

    Unix permission bits.

  • modified?: Date

    The entry's modification time.

class ZipEntry

A single file or directory inside a ZIP archive. Instances are produced by ZipBuffer and ZipFile, or created directly for writing with ZipEntry.create()/ZipEntry.createStream().

create() and content() each have a *Sync counterpart (the streaming contentIterator() does not). As with the synchronous node:fs APIs, these block the Node.js event loop and further JavaScript execution until the operation (including any deflate/inflate pass) 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
  • readonly compressed: boolean

    true if the entry's content is stored in compressed form (any compression method, currently deflate or Zstandard); false if it is stored uncompressed.

  • readonly compressedSize: number
  • readonly crc32: number
  • readonly flags: number

    The entry's raw general-purpose bit flag.

  • readonly isDirectory: boolean

    true if the entry is a directory (its name ends with /).

  • readonly isFile: boolean

    true if the entry is a regular file - that is, neither a directory nor a symbolic link.

  • readonly method: number

    The entry's raw compression method: 0 for stored, 8 for deflate, 93 for Zstandard.

  • readonly mode: number

    The entry's Unix mode permission bits, including the setuid, setgid, and sticky bits (the low 12 bits, 0o7777), or 0 if the archive was not written on a Unix-like system. The file-type bits are not included here; use zipEntry.isDirectory / zipEntry.isSymlink for the type.

  • readonly modified: Date

    The entry's last-modification time. When the archive carries a higher-fidelity timestamp in an extra field - an NTFS (0x000a), Info-ZIP extended (0x5455), or Info-ZIP Unix (0x5855) field, as most modern tools write - that absolute (UTC) time is used; otherwise the coarse, local-time MS-DOS date/time field (2-second resolution) is used.

    Some tools store their high-fidelity timestamp only in the local file header, so on a file-backed entry (one returned by zipFile.get()) the first read of this property may perform a small synchronous positioned disk read to resolve that header. If that read fails, the value silently falls back to the central-directory data.

  • readonly name: string

    The entry's name, decoded from the central directory, which is treated as authoritative - a local file header that disagrees is ignored, so a mismatched-header ("ZIP-confusion") archive cannot make name disagree with what is read. The bytes are decoded from a valid Info-ZIP Unicode Path extra field (0x7075) when one is present; otherwise as UTF-8 when the language-encoding flag (general-purpose bit 11) is set or the bytes are valid UTF-8 (plenty of tools wrote UTF-8 names without ever setting the flag); and as CP437 - the historical default - only when they are not. See zipEntry.nameBuffer for the raw bytes.

    The name is returned verbatim: it is never normalized, and a name containing .., a leading /, a drive letter, or backslashes is neither rewritten nor rejected. A ZipFile/ZipBuffer never writes to disk, so guarding against path traversal ("Zip Slip") when extracting is the caller's responsibility.

  • readonly nameBuffer: Buffer

    The entry's raw name bytes, before any character decoding. Useful when the archive's names are in an encoding other than UTF-8 or CP437 and the caller wants to decode them itself.

  • readonly rawContent: null | Buffer<ArrayBufferLike>

    The entry's raw (still compressed, if applicable) content when it is held in memory, or null when there is no in-memory buffer to expose - for an entry created with zlib.ZipEntry.createStream(), or a file-backed entry returned by zipFile.get(), whose bytes are read from disk on demand rather than retained. Use zipEntry.content() or zipEntry.contentIterator() to read a file-backed entry.

  • readonly size: number

    The entry's uncompressed size, in bytes.

  • [Symbol.asyncDispose](): Promise<void>;
  • ): Promise<NonSharedBuffer>;

    Throws an ERR_ZIP_ENTRY_TOO_LARGE error if the entry's declared size exceeds maxSize, an ERR_ZIP_ENTRY_CORRUPT error if the content fails CRC-32 verification or does not match its declared size, and an ERR_INVALID_STATE error for a streaming entry (zlib.ZipEntry.createStream()) whose content is not yet available (see that method for when a streaming entry becomes readable).

    @returns

    Promise fulfilled with a Buffer containing the entry's decompressed content. The buffer is a fresh copy that shares no memory with the archive or with data the entry was created from.

  • ): AsyncIterator<Buffer<ArrayBufferLike>>;

    Unlike zipEntry.content(), this does not buffer the whole member in memory. For a file-backed entry (one returned by zipFile.get()) the compressed bytes are read from disk as the iterator is consumed and nothing is retained; the entry is valid only while its ZipFile is open.

    Because streaming is the bounded-memory path for arbitrarily large members, it is not capped by zlib.getMaxZipContentSize() the way zipEntry.content() is - that default guards a single large allocation, which streaming never makes. Output is still bounded per chunk to the declared uncompressed size; pass maxSize to impose an explicit ceiling.

    For an in-memory entry stored without compression, the yielded chunks are zero-copy views of the entry's retained content (see zipEntry.rawContent); do not mutate them.

    The yielded chunks are provisional until the iterator completes. CRC-32 verification (and the final declared-size check) can only run once every byte has been read, so a corrupt or truncated entry is reported by the iterator throwing after the last chunk, not before the first. Each chunk is still bounded so the total never exceeds the declared size or maxSize, but a consumer that must not act on unverified bytes should buffer them (or use zipEntry.content(), which verifies before returning anything) rather than processing chunks as they arrive.

    @returns

    AsyncIterator of Buffer chunks of the entry's decompressed content.

  • ): NonSharedBuffer;

    The synchronous version of zipEntry.content().

    @param options

    See zipEntry.content().

    @returns

    The entry's decompressed content.

  • static create(
    filename: string,
    ): Promise<ZipEntry>;

    Compresses data (unless method is 'store', or compression would not reduce its size) and computes its CRC-32.

    When the entry ends up stored uncompressed (because method is 'store', or because compression would not reduce the size), the entry retains a zero-copy view of data rather than a copy, and its CRC-32 has already been recorded. Do not mutate data after creating the entry; pass a copy if it might change.

    The MS-DOS date/time fields ZIP uses for modified have 2-second resolution and no time zone. When modified does not fall on a whole 2-second boundary, an Info-ZIP extended-timestamp extra field is written as well, recording the whole (UTC) second so the time round-trips more precisely (see zipEntry.modified). This applies to every entry-creation path.

    @param filename

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

    @param data

    The entry's complete, uncompressed content. Must be empty when filename names a directory.

  • static createStream(
    filename: string,
    source: AsyncIterable<Uint8Array<ArrayBufferLike>>,

    Creates an entry whose content is compressed on the fly as it is serialized by zlib.createZipArchive(), without buffering source in memory. Its size, compressedSize, and crc32 only become available once serialization has finished. There is no synchronous counterpart: streaming entries only make sense with an asynchronous, incrementally-produced source.

    source is drained exactly once, during serialization. Until that happens the entry has no readable content, so zipEntry.content(), zipEntry.contentSync(), and zipEntry.contentIterator() throw ERR_INVALID_STATE. If the entry is serialized by adding it to a writable ZipFile with zipFile.addEntry() (or addEntrySync()), it is then promoted in place to a file-backed entry pointing at the copy just written, so it becomes readable (and can be serialized again) for as long as that ZipFile stays open. Serializing it any other way (for example directly through zlib.createZipArchive()) leaves it spent and unreadable.

    Because source may hold an operating-system resource (a file read stream, say), a streaming entry is disposable: its Symbol.dispose and Symbol.asyncDispose methods destroy source if it has not been consumed. An entry passed to an archive is disposed by that archive (see zlib.createZipArchive()); dispose an entry directly only when it was built but never handed to one. Disposal is a no-op for non-streaming entries - in particular a file-backed entry never closes the ZipFile descriptor it borrows.

    @param filename

    The entry's name within the archive. Must not end in /.

    @param source

    Yields the entry's uncompressed content as Uint8Array chunks.

  • static createSync(
    filename: string,

    The synchronous version of zlib.ZipEntry.create().

    @param filename

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

    @param data

    The entry's complete, uncompressed content. Must be empty when filename names a directory.

    @param options

    See zlib.ZipEntry.create().

  • static read(
    buffer: ZipInputType
    ): Iterator<ZipEntry>;

    Parses every entry out of buffer directly, without indexing it into a ZipBuffer. Like ZipBuffer, the yielded entries hold zero-copy views of buffer rather than copies of their content, so the same rule applies: do not mutate or reuse buffer while any of them is still in use.

    @param buffer

    A complete ZIP archive.

    @returns

    Iterator of ZipEntry.