Archive PRO
The Archive class provides a comprehensive interface for working with archive files (such as ZIP).
It supports reading, creating, updating, and extracting entries from archives, with both asynchronous and synchronous methods.
Overview
Archive enables flexible management of compressed archive contents, including:
- Opening existing archives or creating new ones
- Adding files, directories, or in-memory data
- Extracting entries to memory or disk
- Deleting specific entries
- Listing archive contents
- Supporting multiple compression methods (e.g.
deflate,none) - Working in either synchronous or asynchronous modes
Static Methods
static openForMode(path: string, accessMode: "update" | "read", options?: { pathEncoding?: Encoding }): Archive
Opens an archive file.
Parameters:
Returns:
An Archive object.
Example:
Properties
path: string
The path of the archive file.
Example:
data: Data | null
The raw data of the archive (if opened from memory).
Instance Methods
entries(pathEncoding?: Encoding): ArchiveEntry[]
Retrieves the entries in the archive.
Parameters:
pathEncoding: Optional. The encoding to use for decoding entry paths (default is "utf-8").
Returns:
An array of ArchiveEntry objects.
getEntryPaths(encoding?: Encoding): string[]
Retrieves the paths of all entries in the archive.
Parameters:
encoding: Optional. The encoding to use for decoding entry paths (default is "utf-8").
Returns: An array of entry paths.
getEntry(path: string): ArchiveEntry | null
Retrieves an entry by its path.
Parameters:
path: The path of the entry to retrieve.
Returns:
The ArchiveEntry object if found; otherwise, null.
contains(path: string): boolean
Checks whether the archive contains a specific entry.
Parameters:
path: The path of the entry to check.
Returns:
true if the path exists; otherwise, false.
Example:
addEntry(path: string, toPath: string, options?: { compressionMethod?: "deflate" | "none"; bufferSize?: number }): Promise<void>
Adds an existing file to the archive (asynchronously).
Parameters:
Example:
addEntrySync(path: string, toPath: string, options?)
Synchronous version of addEntry().
Throws an error if the entry cannot be added.
addFileEntry(path: string, uncompressedSize: number, provider: (offset: number, length: number) => Data, options?): Promise<void>
Adds a file entry to the archive using a data provider function (asynchronous).
Parameters:
Example:
addFileEntrySync(...)
Synchronous version of addFileEntry().
addDirectoryEntry(path: string, options?): Promise<void>
Adds a directory entry to the archive.
Parameters:
Example:
addDirectoryEntrySync(...)
Synchronous version of addDirectoryEntry().
removeEntry(path: string, options?): Promise<void>
Removes a specific entry from the archive (asynchronously).
Parameters:
Example:
removeEntrySync(...)
Synchronous version of removeEntry().
extract(path: string, consumer: (data: Data) => void, options?): Promise<void>
Extracts a specific entry from the archive and provides its data in chunks via a consumer callback (asynchronous).
Parameters:
Example:
extractSync(...)
Synchronous version of extract().
extractTo(path: string, to: string, options?): Promise<void>
Extracts an entry or directory from the archive to a specific file system location (asynchronously).
Parameters:
Example:
extractToSync(...)
Synchronous version of extractTo().
ArchiveEntry Interface
ArchiveEntry represents a single entry (file, directory, or symbolic link) inside an archive.
Example:
