Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaJavaScriptNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Node.js Examples
Web API Categories

AI
ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Apple Keychain
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Box
CAdES
CSR
CSV
Cert Store
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
JavaScript
MHT / HTML Email
MIME
Markdown
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
Regular Expressions
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
X
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Node.js) Create a ZIP Archive with a Top-Level Directory Using PathPrefix

This example demonstrates how to use the PathPrefix property to ensure that all files in a ZIP archive are stored beneath a common top-level directory.

The example recursively adds files from:

c:/zipTest/directory_name

And stores them in the ZIP archive beneath:

directory_name/

For example, the local filesystem file:

c:/zipTest/directory_name/hello.txt

Will be stored in the ZIP archive as:

directory_name/hello.txt

This is useful when creating ZIP archives that should extract into a single parent directory rather than directly into the current extraction location.

The example also demonstrates that methods such as AppendFiles initially add references to filesystem files, and that the actual file reading and compression occur later when WriteZipAndClose is called.

Install Chilkat for Node.js and Electron using npm at

Chilkat npm packages for Node.js

Chilkat npm packages for Electron

on Windows, Linux, MacOS

var os = require('os');
if (os.platform() == 'win32') {  
  var chilkat = require('@chilkat/ck-node23-win64'); 
} else if (os.platform() == 'linux') {
    if (os.arch() == 'arm') {
        var chilkat = require('@chilkat/ck-node23-linux-arm');
    } else if (os.arch() == 'arm64') {
        var chilkat = require('@chilkat/ck-node23-linux-arm64');
    } else {
        var chilkat = require('@chilkat/ck-node23-linux-x64');
    }
} else if (os.platform() == 'darwin') {
  var chilkat = require('@chilkat/ck-node23-mac-universal');
}

function chilkatExample() {

    var success = false;

    var zip = new chilkat.Zip();

    // Initialize the Zip object and set the output filename.
    // The .zip file is not created until WriteZip or WriteZipAndClose is called.
    success = zip.NewZip("qa_output/test.zip");
    if (success == false) {
        console.log(zip.LastErrorText);
        return;
    }

    // Prepend "directory_name/" to each ZIP entry added after this point.
    // 
    // For example, a file found at:
    // 
    //     c:/zipTest/directory_name/hello.txt
    // 
    // Will be stored in the ZIP as:
    // 
    //     directory_name/hello.txt
    // 
    // This is useful when you want the files to unzip beneath a specific
    // top-level directory.
    zip.PathPrefix = "directory_name/";

    // Recursively add references to all files beneath:
    // 
    //     c:/zipTest/directory_name
    // 
    // AppendFiles does not immediately read or compress the files.
    // It adds references to files in the local filesystem.
    // The referenced files are read and compressed when WriteZipAndClose is called.
    var recurse = true;

    success = zip.AppendFiles("c:/zipTest/directory_name/*",recurse);
    if (success == false) {
        console.log(zip.LastErrorText);
        return;
    }

    // Write the ZIP archive and close it.
    success = zip.WriteZipAndClose();
    if (success == false) {
        console.log(zip.LastErrorText);
        return;
    }

    console.log("Success.");

}

chilkatExample();

 

© 2000-2026 Chilkat Software, Inc. All Rights Reserved.