How to Easily Create and Extract Zip Archives with APIs in Nodejs

Cloudmersive
5 min readMay 23, 2024

--

With APIs, we can easily automate the process of creating zip archives and extracting archive contents in our Node.js applications.

Handling our Zip archives with APIs can, for example, speed up the deployment of zipped applications. It can also facilitate more efficient integrations with external systems that use zip archives as standard data exchange formats — among other advantages.

We can easily structure API calls using complementary, ready-to-run code examples provided further down the page. We can authorize both API calls with the same API key (note that free API keys will allow a limit of 800 API calls per month), and we can install the client SDK with a single NPM command.

Client SDK Installation

To install the client SDK, let’s run the following NPM command:

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

We can, alternatively, add the Node client to our package.json:

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

Now we can copy from the code examples provided below to make various value-add API calls. In each code block, we can replace the ‘YOUR API KEY’ placeholder string with our own API key string to authorize our requests.

Creating a Standard or Secured Zip Archive

Using the following code, we can compress files to create a standard zip archive:

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 inputFile1 = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | First input file to perform the operation on.

var opts = {
'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.zipArchiveZipCreate(inputFile1, opts, callback);

If we want to create an encrypted & password-protected version of our archive, we can use the following code instead. This allows us to set a password string and select from AES-256, AES-128 or PK-Zip encryption algorithms:

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 secure

var 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);

Creating an Advanced Zip Archive

Using the following code examples, we can create a zip archive with advanced options. This means we can control the structure of our resulting archive via special parameters in our input request:

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 request = new CloudmersiveConvertApiClient.CreateZipArchiveRequest(); // CreateZipArchiveRequest | Input request


var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.zipArchiveZipCreateAdvanced(request, callback);

We can structure our advanced archive input request following the below example JSON model:

{
"FilesInZip": [
{
"FileName": "string",
"FileContents": "string"
}
],
"DirectoriesInZip": [
{
"DirectoryName": "string",
"DirectoriesInDirectory": [
null
],
"FilesInDirectory": [
{
"FileName": "string",
"FileContents": "string"
}
]
}
]
}

Securing an Existing Zip Archive

We can use the below code to apply encryption and password protection to an existing archive in our system. We could, for example, use this in conjunction with the Advanced Zip Archive request to create secure, advanced archives:

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 encryptionRequest = new CloudmersiveConvertApiClient.ZipEncryptionAdvancedRequest(); // ZipEncryptionAdvancedRequest | Encryption request


var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.zipArchiveZipEncryptAdvanced(encryptionRequest, callback);

We can structure our request following the below example JSON model:

{
"InputFileContents": "string",
"Password": "string",
"EncryptionAlgorithm": "string"
}

Extracting Standard or Secure Zip Archives

We can then use the following code to extract our standard zip archives:

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 inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | 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.zipArchiveZipExtract(inputFile, callback);

And we can use the below examples to decrypt and remove password protection from our secured zip archives:

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 inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.

var zipPassword = "zipPassword_example"; // String | Required; Password for the input archive


var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.zipArchiveZipDecrypt(inputFile, zipPassword, callback);

Conclusion

That’s all the code we’ll need! We can now automate important Zip Archive creation and extraction processes in our Node.js applications.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet