How to Unzip Files with JavaScript in NodeJS
Why Unzip Files with JavaScript?
The pdfRest Zip Files API Tool is a powerful resource for developers looking to manage file compression and decompression through API calls. This tutorial will guide you through the process of sending an API call to unzip files using JavaScript. By leveraging this tool, you can automate the handling of zip files within your applications, making it easier to manage and process large sets of data.
You might use the Zip Files API to handle multiple document uploads on a web application. For instance, a user could upload a zip file containing several PDFs, and your application could automatically unzip and process each file individually. This can be particularly useful in environments where file management efficiency is crucial, such as in document management systems or data analysis platforms.
Unzip Files with JavaScript Code Example
/** * This request demonstrates how to unzip a file */ var axios = require("axios"); var FormData = require("form-data"); var fs = require("fs"); // Create a new form data instance and append the PDF file and parameters to it var data = new FormData(); data.append("file", fs.createReadStream("/path/to/file")); // define configuration options for axios request var config = { method: "post", maxBodyLength: Infinity, // set maximum length of the request body url: "https://api.pdfrest.com/unzip", 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.
Source: GitHub Repository
Breaking Down the Code
The provided code begins by importing necessary modules: axios
for making HTTP requests, form-data
for handling form submissions, and fs
for file system operations.
var data = new FormData(); data.append("file", fs.createReadStream("/path/to/file"));
This snippet creates a new FormData
instance and appends a file to it. The fs.createReadStream
method is used to read the file located at /path/to/file
. Ensure to replace this path with the actual path of your zip file.
var config = { method: "post", maxBodyLength: Infinity, url: "https://api.pdfrest.com/unzip", headers: { "Api-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", ...data.getHeaders(), }, data: data, };
This section defines the configuration for the HTTP request. The method
is set to post
, and maxBodyLength
is set to Infinity
to accommodate large files. The url
specifies the endpoint for unzipping files. The headers
include an Api-Key
which must be replaced with your actual API key. The data
field contains the form data to be sent in the request.
axios(config) .then(function (response) { console.log(JSON.stringify(response.data)); }) .catch(function (error) { console.log(error); });
This part sends the request using axios
. If the request is successful, the response data is logged to the console. If there is an error, it is caught and logged instead.
Beyond the Tutorial
In this tutorial, you learned how to use JavaScript to make an API call to the pdfRest Zip Files endpoint, allowing you to unzip files programmatically. This can significantly enhance your application's ability to manage and process files efficiently.
To explore more capabilities of the pdfRest API, try out all the tools available in the API Lab. For detailed documentation, refer to the API Reference Guide.
Note: This example demonstrates a multipart API call. For code samples using JSON payloads, visit this GitHub repository.