How to Encrypt a ZIP File in Node.JS

Cloudmersive
2 min readNov 5, 2020

Working with and sharing large files can be vastly simplified through the compression of your files into the ZIP file format. Furthermore, these files can then be easily protected through encryption and password-protection, ensuring that your information cannot be accessed outside of your knowledge. The following API will allow you to create new ZIP files and apply encryption and password protection automatically. The parameters necessary for running this function include the desired password and your input file; note that you are able to input up to ten files by adding new input file values within the function. You may also optionally specify the encryption algorithm to AES-256, AES-128, and PK-Zip, with a default value of AES-256.

To use this API, run the following command to install the SDK:

npm install cloudmersive-convert-api-client --save

Or, you can add this snippet to your package.json:

"dependencies": {
"cloudmersive-convert-api-client": "^2.4.8"
}

Then, you can call the function:

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;
// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';
var apiInstance = new CloudmersiveConvertApiClient.ZipArchiveApi();var password = "password_example"; // String | Password to place on the Zip file; the longer the password, the more securevar inputFile1 = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | First input file to perform the operation on.var opts = {
'encryptionAlgorithm': "encryptionAlgorithm_example", // String | Encryption algorithm to use; possible values are AES-256 (recommended), AES-128, and PK-Zip (not recommended; legacy, weak encryption algorithm). Default is AES-256.
'inputFile2': Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer), // File | Second input file to perform the operation on.
'inputFile3': Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer), // File | Third input file to perform the operation on.
'inputFile4': Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer), // File | Fourth input file to perform the operation on.
'inputFile5': Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer), // File | Fifth input file to perform the operation on.
'inputFile6': Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer), // File | Sixth input file to perform the operation on.
'inputFile7': Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer), // File | Seventh input file to perform the operation on.
'inputFile8': Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer), // File | Eighth input file to perform the operation on.
'inputFile9': Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer), // File | Ninth input file to perform the operation on.
'inputFile10': Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer) // File | Tenth input file to perform the operation on.
};
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.zipArchiveZipCreateEncrypted(password, inputFile1, opts, callback);

You can retrieve the API Key for this function at no cost and with no commitment on the Cloudmersive website. This will give you access to 800 monthly calls across our entire API library.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.