How to Extract, Decompress Files from a Zip Archive using Node.js
With low-code API solutions, we can expand the functionality of our applications in minutes.
Using the below code, we can incorporate a free API into our application that extracts and decompresses files in a zip archive, returning file name/file contents objects and/or any file subdirectories stored in the archive. We can utilize this service for free with a free-tier API key — this will allow us to make up to 800 API calls per month with no additional commitments.
We can begin structuring our API call by installing the SDK. Let’s go ahead and run the following command:
npm install cloudmersive-convert-api-client --save
Alternatively, we can install the SDK by adding the below snippet to our package.json:
"dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}
Now we can add the below code to our file and authorize our request with our free-tier API key:
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);
That’s all there is to it — we can now quickly extract and load Zip Archive contents with a low-code API solution.