@zip.js/zip.js
    Preparing search index...

    Class ZipReader<Type>

    Represents an instance used to read a zip file.

    Here is an example showing how to read the text data of the first entry from a zip file:

    // create a BlobReader to read with a ZipReader the zip from a Blob object
    const reader = new zip.ZipReader(new zip.BlobReader(blob));

    // get all entries from the zip
    const entries = await reader.getEntries();
    if (entries.length) {

    // get first entry content as text by using a TextWriter
    const text = await entries[0].getData(
    // writer
    new zip.TextWriter(),
    // options
    {
    onprogress: (index, max) => {
    // onprogress callback
    }
    }
    );
    // text contains the entry data as a String
    console.log(text);
    }

    // close the ZipReader
    await reader.close();

    Type Parameters

    • Type
    Index
    • Creates the instance

      Type Parameters

      • Type

      Parameters

      Returns ZipReader<Type>

      Reading a zip file requires random access because the central directory located at the end of the file is read first. A ReadableStream instance, or an object providing only a readable property (e.g. a file handle), is therefore buffered entirely in memory when the instance is initialized. To read a large seekable resource without buffering it, pass a custom Reader implementation that reads the requested byte ranges directly, or a lazily-read Blob instance when the runtime provides one, e.g. await fs.openAsBlob(path) on Node.js or Bun.file(path) on Bun. See Reader for an example reading a Deno.FsFile with random access.

    appendedData?: Uint8Array<ArrayBufferLike>

    The data appended after the zip file.

    comment: Uint8Array

    The global comment of the zip file.

    Unlike EntryMetaData#comment, it is exposed as raw bytes because the zip format defines no way to record its encoding: section 4.4.26 of the zip specification says nothing about it, and the end of central directory record has neither a general purpose bit flag nor an extra field, so the language encoding flag (see Appendix D - Language Encoding (EFS)) cannot apply to it. Decode it with the encoding agreed with the producer of the zip file.

    digitalSignature?: Uint8Array<ArrayBufferLike>

    The data of the digital signature record of the central directory (see ZipWriterCloseOptions#signCentralDirectory), if the zip file contains one.

    zip.js does not verify signatures. The signed data is the central directory records, read at ZipReader#directoryOffset, and it never includes the digital signature record itself. Some writers (e.g. SecureZIP) store that record inside ZipReader#directoryLength, so verifying the whole declared range would always fail.

    directoryLength?: number

    The length in bytes of the central directory as declared in the end of central directory record. Some writers (e.g. SecureZIP) include the digital signature record in that length, so subtract 6 + digitalSignature.length from it when the record is stored inside the declared range.

    directoryOffset?: number

    The offset of the central directory in the zip file.

    prependedData?: Uint8Array<ArrayBufferLike>

    The data prepended before the zip file.

    warnings?: ArchiveWarning[]

    The non-fatal diagnostics deposited while reading the entries, replaced every time ZipReader#getEntries or ZipReader#getEntriesGenerator runs.

    A warning reports a characteristic of the zip file observed in data the parse had already read: depositing one never costs additional I/O, and a well-formed zip file deposits none. Each ArchiveWarning#reason value is deposited at most once per call, with ArchiveWarning#filename naming the first entry it applies to when it applies to an entry.

    Two kinds of reasons are deposited. Observations are always non-fatal: WARNING_UNSORTED_CENTRAL_DIRECTORY, WARNING_UNKNOWN_VERSION (the low byte of the "version needed to extract" field exceeds the highest known zip specification version; the high byte is ignored because some writers store a host identifier in it), WARNING_COMPRESSED_PATCHED_DATA (bit 5 of the general purpose bit flag), WARNING_MALFORMED_EXTRA_FIELD, WARNING_UNKNOWN_ZIP64_EXTENSIBLE_DATA, WARNING_WRAPPED_ENTRIES_COUNT and WARNING_PREPENDED_CENTRAL_DIRECTORY (the prepended data holds a central directory of its own, i.e. another archive precedes this one and other readers may report its entries instead). The other reasons are the checks that strictness: "strict" rejects with ERR_AMBIGUOUS_ARCHIVE: when the effective strictness tolerates one of them and the evidence is already in hand, the same reason string is deposited as a warning instead — WARNING_APPENDED_DATA, WARNING_PREPENDED_DATA, WARNING_TRAILING_CENTRAL_DIRECTORY_DATA, WARNING_DUPLICATE_FILENAME and WARNING_MISMATCHED_ZIP64_END_OF_CENTRAL_DIRECTORY. WARNING_MULTIPLE_END_OF_CENTRAL_DIRECTORY is the one reason of that group which is never tolerated, so it is only ever the reason of an error.

    The warnings related to the local file header of an entry are deposited on EntryMetaData#warnings when its data is read, not here.

    • Calls ZipReader#close, making the instance usable with await using

      Returns Promise<void>

      The method is only defined when the runtime provides Symbol.asyncDispose. Its declaration is ignored by TypeScript versions that do not declare the symbol either, i.e. before 5.2 or without the esnext.disposable library, so that the declarations of the library keep compiling there.

    • Closes the zip file

      Returns Promise<void>

      It cancels the ReadableStream instance passed to the constructor when nothing has been read from it, which is the only resource a ZipReader instance can hold. It does nothing otherwise: the stream is already consumed once ZipReader#getEntries has read the entries into memory, and the Reader instances are never closed, they belong to the caller. The entries returned by ZipReader#getEntries can therefore still be read after calling it.