How to Unzip Files with JavaScript in NodeJS

Learn how to unzip files from .zip archives using the pdfRest Zip Files API Tool with JavaScript.
Share this page

Why Unzip Files with JavaScript?

The pdfRest Zip Files API Tool offers a robust solution for managing and manipulating zip files through API calls. This tutorial will guide you through the process of sending an API call to unzip files using JavaScript, leveraging the capabilities of pdfRest.

Users often need to extract contents from zip files to facilitate efficient processing with pdfRest. By starting with a .zip archive of files, developers can automate data processing tasks, integrate seamlessly with other applications, and manage large datasets more effectively. The Zip Files API simplifies these processes, enabling developers to handle compressed files programmatically and streamline workflows.

Unzip Files with JavaScript Code Example

const fetch = require('node-fetch');
const fs = require('fs');
const FormData = require('form-data');

const form = new FormData();
form.append('file', fs.createReadStream('path/to/your/file.zip'));

const options = {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Api-Key': 'your_api_key_here'
  },
  body: form
};

fetch('https://api.pdfrest.com/unzip', options)
  .then(response => response.json())
  .then(response => console.log(response))
  .catch(err => console.error(err));

Source: GitHub Repository

Breaking Down the Code

The code begins by importing necessary modules: node-fetch for making HTTP requests, fs for file system operations, and form-data for handling form data.

const fetch = require('node-fetch');
const fs = require('fs');
const FormData = require('form-data');

Next, a new FormData object is created, and the zip file to be unzipped is appended to it. The path to the zip file is specified, and fs.createReadStream is used to read the file.

const form = new FormData();
form.append('file', fs.createReadStream('path/to/your/file.zip'));

The options object specifies the HTTP method as POST, sets the headers including the Api-Key, and assigns the form as the body of the request.

const options = {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Api-Key': 'your_api_key_here'
  },
  body: form
};

The fetch function is used to send the request to the https://api.pdfrest.com/unzip endpoint. The response is processed as JSON, and the result is logged to the console. Any errors encountered during the request are caught and logged.

fetch('https://api.pdfrest.com/unzip', options)
  .then(response => response.json())
  .then(response => console.log(response))
  .catch(err => console.error(err));

Beyond the Tutorial

In this tutorial, you learned how to make an API call to unzip files using JavaScript with the pdfRest Zip Files API Tool. This process can be integrated into larger applications where automated file extraction is needed.

To explore more capabilities of pdfRest, try out all the API Tools in the API Lab. For detailed information on each API endpoint, refer to the API Reference Guide.

Note: This example demonstrates a multipart API call. For code samples using JSON payloads, visit the GitHub Repository.

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