How to Zip Files with JavaScript in NodeJS

Learn how to use pdfRest Zip Files API Tool with JavaScript in NodeJS to zip processed files for easy downloads.
Share this page

Why Use Zip Files with JavaScript?

The pdfRest Zip Files API Tool is a powerful resource for developers looking to compress multiple files into a single ZIP archive through an API call.

This tutorial will demonstrate how to use JavaScript to send an API call to the Zip Files endpoint provided by pdfRest, specifically targeting the use case of compressing files programmatically.

In the real world, a user might need to zip files to reduce the overall file size for storage or to simplify the process of downloading multiple files by consolidating them into a single download. For instance, a web application could allow users to select multiple documents and download them as one ZIP file, or a server-side script might automatically archive a batch of reports at the end of each day.

Zip Files with JavaScript Code Example

/**
 * This request demonstrates how to compress files into one ZIP file.
 * To accommodate additional files, add additional file and/or id[] form-data body parameters.
 */
var axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');

// Create a new form data instance and append the files and parameters to it
var data = new FormData();
data.append('file', fs.createReadStream('/path/to/file'));
data.append('file', fs.createReadStream('/path/to/file'));
data.append('output', 'pdfrest_zip');

// define configuration options for axios request
var config = {
  method: 'post',
maxBodyLength: Infinity, // set maximum length of the request body
  url: 'https://api.pdfrest.com/zip',
  headers: { 
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', // Replace with your API key
    ...data.getHeaders() // set headers for the request
  },
  data : data // set the data to be sent with the request
};

// send request and handle response or error
axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error);
});

// If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.js' sample.

This code is sourced from the pdf-rest-api-samples repository on GitHub.

Breaking Down the Code

The code uses the Axios library to send HTTP requests and the FormData library to construct multipart/form-data payloads. Here's a breakdown of the key parts:

var axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');

This initializes the required modules: Axios for making the HTTP call, FormData to build the form data, and fs (File System) to read files from the system.

var data = new FormData();
data.append('file', fs.createReadStream('/path/to/file'));
data.append('output', 'pdfrest_zip');

Here, a new FormData instance is created, and files are appended to it. The 'file' fields are the files to be zipped, and 'output' is the desired name of the resulting ZIP file.

var config = {
  method: 'post',
  maxBodyLength: Infinity,
  url: 'https://api.pdfrest.com/zip',
  headers: { 
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
    ...data.getHeaders()
  },
  data : data
};

The config object contains the Axios request configuration. The 'Api-Key' header should be replaced with your actual API key. The 'maxBodyLength' property is set to Infinity to allow for large file sizes.

axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error);
});

The Axios instance is called with the config object, and the promise handlers log the response or error to the console.

Beyond the Tutorial

In this tutorial, we've learned how to use JavaScript to make an API call to the pdfRest Zip Files endpoint to compress files into a ZIP archive. This is particularly useful for applications that need to handle file compression on the fly.

To explore more capabilities and demo all of the pdfRest API Tools, visit the API Lab. For further details on how to use the API, refer to the API Reference Guide.

Note that this is an example of a multipart API call. Code samples using JSON payloads for other operations can be found at the pdf-rest-api-samples repository on GitHub.

Generate a self-service API Key now!

Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.

Compare Plans
Contact Us