Unzip (extract an archive)
function Unzip(zipArchive: string, toWhere: string, password?: string): boolean;Extracts all files and directories from a zip archive into the destination directory toWhere. The destination directory must already exist. Returns true if every entry was extracted successfully, false if any step failed.
When password is provided, the archive is treated as an encrypted zip (as created by Zip() with a password). Omit password or pass an empty string for plain (non-encrypted) archives.
| Parameter | Type | Requirement | Explanation |
|---|---|---|---|
zipArchive | string | required | Fully qualified path to the zip file to extract |
toWhere | string | required | Path to an existing directory where the archive contents will be placed |
password | string | optional | Decryption password for encrypted archives; omit for plain archives |
| Return value | Explanation |
|---|---|
true | All entries extracted successfully |
false | The archive could not be opened, a file could not be written, or a path traversal attack was detected |
NOTE
Unzip is the counterpart of Zip. You can use them together to archive files, move or transfer the archive, and then extract it at the destination.
IMPORTANT
Unzip rejects archive entries whose paths would escape toWhere (the "zip-slip" attack). Such archives will cause Unzip to return false and log an error without extracting any further entries.
Examples
// Extract a plain archive
var ok = Unzip('/var/inbox/data.zip', '/var/processed/');
if (!ok) {
Log('Extraction failed.');
}
// Extract a password-protected archive
var ok = Unzip('/var/inbox/secure.zip', '/var/processed/', GetSecret('zip-pass'));
if (!ok) {
Log('Encrypted extraction failed.');
}Archive-and-extract pattern
// 1. Compress the outbox into an archive
Zip('/var/outbox/*.csv', '/var/staging/export.zip');
// 2. Transfer the archive (e.g. via a remote client) ...
// 3. Extract at the destination
Unzip('/var/staging/export.zip', '/var/delivered/');