How to Compress Files, Create a New Zip Archive in Node.js

Cloudmersive
2 min readNov 3, 2023

--

Zip archives make it drastically easier to share or export large volumes of content, and incorporating a Zip archive workflow in our application can further boost our productivity.

Using the below code, we can take advantage of a free API that quickly compresses 10 or more files into a new zip archive. We can utilize this service up to 800 times per month with a free-tier API key, with no commitments upon reaching that limit (our total will reset for the following month).

We can structure our API call in a few quick steps, starting with SDK installation. We can either install the SDK by running this command:

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

Or by adding this snippet to our package.json:

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

Lastly, we can include the below code examples in our file & copy in our API key to start compressing files:

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

That’s all there is to it — no more code required!

--

--

Cloudmersive

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