zip
The archive/zip module creates and extracts ZIP archives in JSH.
It provides in-memory helpers, stream-style APIs, and a file-based Zip class.
Use the API that matches your task:
- Use
zipSync()andunzipSync()when the archive is already in memory. - Use
Zipwhen you want to read files, save.zipfiles, or extract them to disk. - Use
createZip()andcreateUnzip()when you want event-driven processing.
Installation
const zip = require('archive/zip');zipSync()
Creates a ZIP archive synchronously.
Syntax
zipSync(data)Parameters
dataString | ArrayBuffer | Uint8Array | Number[] | Object[]
If data is a single string or byte buffer, the created entry name defaults to data.
If data is an array, each item should be an entry object such as { name, data }.
Return value
Returns an ArrayBuffer that contains ZIP archive bytes.
This form is useful for quick tests or for building an archive before writing it somewhere else.
Usage example
| |
| |
unzipSync()
Extracts ZIP archive bytes synchronously and returns entry objects.
Syntax
unzipSync(buffer)Parameters
bufferArrayBuffer | Uint8Array | Number[]
Return value
Returns an array of entry objects.
Each entry can include name, data, comment, method, compressedSize, size, isDir,
and modified.
Usage example
| |
zip()
Provides a callback-style asynchronous wrapper for ZIP archive creation.
Syntax
zip(data, callback)The callback follows the (err, archive) form.
unzip()
Provides a callback-style asynchronous wrapper for ZIP extraction.
Syntax
unzip(buffer, callback)The callback follows the (err, entries) form.
Use these wrappers when you want callback-based control flow without managing stream events directly.
Usage example
| |
createZip()
Creates a stream-style ZIP writer.
The returned object accepts entry objects through write() and emits archive bytes through the
data event when end() is called.
This API is event-driven, but it still finalizes work on end() rather than emitting extracted
files progressively like a Node.js file stream.
Syntax
createZip()createUnzip()
Creates a stream-style ZIP reader.
Write archive bytes with write(), then call end() to emit one entry event per extracted item.
Syntax
createUnzip()Usage example
| |
Zip
Zip is a file-oriented helper class for building, saving, loading, and extracting ZIP archives.
Constructor
new zip.Zip(filePath?)If filePath is provided, the archive is loaded from that file.
addFile()
Reads a file from the filesystem and appends it as an archive entry.
addFile(filePath[, entryName])addBuffer()
Appends a string or byte buffer as an archive entry.
addBuffer(data, entryName[, options])addEntry()
Appends an archive entry object directly.
addEntry(entry)Supported ZIP entry fields:
nameStringrequired entry pathdataString | ArrayBuffer | Uint8Array | Number[]file contentcommentStringentry commentmethodNumbercompression method
addFile() is the most direct option when the source data already exists on disk.
addBuffer() is useful when the content is generated in memory.
addEntry() is helpful when you also want to attach entry metadata such as comment.
getEntries()
Returns a shallow copy of the current archive entries.
getEntries()writeTo()
Writes the archive to a file.
writeTo(filePath)extractAllTo()
Extracts entries to a directory.
extractAllTo(outputDir[, overwrite])
extractAllTo(outputDir, options)
extractAllTo(outputDir, overwrite, options)options supports:
overwriteBooleanoverwrite existing files whentruefilterFunction | RegExp | String | String[]selects which entries to extract
Usage example
| |
Usage example: create from files and extract again
| |
Usage example: filter extraction
| |
Notes
filtermay be a callback,RegExp, string match, or array of entry names.extractAllTo()throws an error if the destination file already exists andoverwriteisfalse.- ZIP entry metadata includes
comment,method,compressedSize, andsizewhen available. - ZIP entries do not support TAR link metadata such as
symlinkorlinkname.