method
zlib.ZipFile.openSync
The synchronous version of zlib.ZipFile.open().
Referenced types
interface ZipFileOpenOptions
- writable?: boolean
Open the underlying file for both reading and writing (
'r+'), enablingzipFile.addEntry(),zipFile.add(), andzipFile.delete().
class ZipFile
A random-access view over the entries of a ZIP archive on disk. Only the archive's tail and central directory are read up front; member content is read from disk lazily, on demand. Writable when opened with { writable: true }: zipFile.addEntry()/zipFile.add() append the new member's data where the central directory used to be, then rewrite the central directory immediately after it; zipFile.delete() just rewrites the central directory. Both mean the file is altered as soon as the method's returned Promise fulfills. Deleted or replaced members are left behind as dead space; zipFile.compact() produces a stream with none.
These in-place edits are not crash-atomic. Rewriting the central directory happens in place, so a write that fails partway - the disk fills, the device disconnects, the process is killed - can leave the archive on disk with a partial or missing central directory, i.e. unreadable, even though the member data before it is intact. The rejected call surfaces the underlying error and the ZipFile object is left usable (its in-memory view is not discarded, so a caller can attempt recovery - for example re-writing the entries elsewhere with zipFile.compact()), but that in-memory view may no longer match the bytes on disk. Write to a copy, or compact() into a fresh file, when durability across a failure matters.
As with the synchronous node:fs APIs, every *Sync method blocks 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. A synchronous method throws ERR_INVALID_STATE if called while an asynchronous add(), addEntry(), delete(), or close() on the same ZipFile has not settled yet, since letting the two interleave could corrupt the archive.
- readonly comment: string
The archive-level comment, preserved byte-for-byte across
zipFile.addEntry()/zipFile.delete()calls. 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). - add(filename: string,
Equivalent to
zipFile.addEntry(await zlib.ZipEntry.create(filename, data, options)).@param filenameThe entry's name within the archive. A trailing
/marks a directory entry.@param dataThe entry's complete, uncompressed content.
@param optionsSee
zlib.ZipEntry.create().@returnsPromisefulfilled with the createdZipEntry. Writes
entrywhere the central directory currently starts, then rewrites the central directory to include it, replacing any existing entry of the same name. ThrowsERR_ZIP_NOT_WRITABLEif theZipFilewas not opened with{ writable: true }.The returned (same)
entryis left readable: a streaming entry created withzlib.ZipEntry.createStream(), which would otherwise be spent once serialized, is promoted in place to a file-backed entry pointing at the copy just written (valid while thisZipFileis open). In-memory entries keep their own buffer unchanged.@returnsPromisefulfilled withentry.The synchronous version of
zipFile.addEntry().entrymust not be a pending streaming entry (one created withzlib.ZipEntry.createStream()) - there is no synchronous way to drain its asynchronous source.@returnsentry- filename: string,
The synchronous version of
zipFile.add(). Equivalent tozipFile.addEntrySync(zlib.ZipEntry.createSync(filename, data, options)).@param filenameThe entry's name within the archive. A trailing
/marks a directory entry.@param dataThe entry's complete, uncompressed content.
@param optionsSee
zlib.ZipEntry.createSync().@returnsThe created entry.
Closes the underlying file handle.
The synchronous version of
zipFile.close().- comment?: string): NonSharedBuffer;
The synchronous version of
zipFile.compact(). Does not modify the open file.@param commentAn archive comment.
@returnsThe currently live entries, serialized as a fresh archive with no dead space left by prior
zipFile.addEntry()/zipFile.delete()calls. - name: string): Promise<boolean>;
Rewrites the central directory without writing any new content - the archive does not grow. Throws
ERR_ZIP_NOT_WRITABLEif theZipFilewas not opened with{ writable: true }.@returnsPromisefulfilled withtrueif an entry namednameexisted and was removed,falseotherwise. - @returns
trueif an entry namednameexisted and was removed,falseotherwise. The synchronous version of
zipFile.entries().@returnsIteratorof[name, entry]pairs, whereentryis a resolvedZipEntry(not aPromise).- thisArg?: any): void;
The synchronous version of
zipFile.forEach():callbackis invoked with a resolvedZipEntryinstead of aPromise. - get(name: string
Returns a lazy, file-backed
ZipEntryforname. Nothing is read from disk here and no content is buffered: the returned entry reads (and, forzipEntry.content(), decompresses) its member straight from the file on each access, and theZipFileretains no member content. The entry is valid only while thisZipFileis open. Reading its content later may throwERR_ZIP_ENTRY_TOO_LARGEif the member is too large to hold in a single buffer; usezipEntry.contentIterator()(orzipFile.stream()) instead. ThrowsERR_ZIP_ENTRY_NOT_FOUNDif the archive has no entry namedname. - name: string
The synchronous version of
zipFile.get(). Likeget(), it reads nothing up front and only builds the lazy handle, so it does not itself block on I/O - but reads performed later through the returned entry (such aszipEntry.contentSync()) do; see theZipFileclass documentation note on synchronous methods. - @returns
Iteratorof entry names. - name: string,
Convenience wrapper that resolves to a
ReadableoverzipEntry.contentIterator()ofzipFile.get(name); the compressed bytes are read from disk as the stream is consumed. The returned promise rejects withERR_ZIP_ENTRY_NOT_FOUNDif the archive has no entry namedname.@returnsPromisefulfilled with astream.Readableof the member's decompressed content, without buffering the whole member in memory. The synchronous version of
zipFile.values().@returnsIteratorof resolvedZipEntryvalues (notPromises).- filename: string,
Throws an
ERR_ZIP_ARCHIVE_TOO_LARGEerror if the archive's central directory is too large to buffer in memory. - filename: string,
The synchronous version of
zlib.ZipFile.open().