Introduction to node7z
node7z is a powerful tool for developers looking to incorporate compression and archiving functionalities within their applications. Packed with numerous API methods, node7z makes it easy to handle various archive formats effortlessly.
Getting Started with node7z
First, you need to install the node7z package using npm:
npm install node-7z
Basic Usage
Let’s cover some of the basic functionalities provided by node7z.
Creating an Archive
To create an archive, use the following API:
const { add } = require('node-7z'); const myPromise = add('archive.7z', '*.txt', { recursive: true }); myPromise.then(() => { console.log('Archive created successfully.'); }).catch(err => { console.error('Error creating archive:', err); });
Extracting an Archive
To extract an archive, use the following code:
const { extract } = require('node-7z'); const myPromise = extract('archive.7z', 'output-directory/'); myPromise.then(() => { console.log('Extraction completed.'); }).catch(err => { console.error('Error extracting archive:', err); });
Listing Archive Contents
You can list the contents of an archive using:
const { list } = require('node-7z'); const myPromise = list('archive.7z'); myPromise.then(list => { console.log('List of files:', list); }).catch(err => { console.error('Error listing files:', err); });
An App Example Using node7z
Let’s build a simple Node.js app that creates, extracts, and lists the contents of an archive file.
const express = require('express'); const { add, extract, list } = require('node-7z'); const app = express(); app.get('/create', (req, res) => { add('archive.7z', '*.txt').then(() => { res.send('Archive created successfully.'); }).catch(err => { res.status(500).send('Error creating archive.'); }); }); app.get('/extract', (req, res) => { extract('archive.7z', 'output-directory/').then(() => { res.send('Extraction completed.'); }).catch(err => { res.status(500).send('Error extracting archive.'); }); }); app.get('/list', (req, res) => { list('archive.7z').then(list => { res.json(list); }).catch(err => { res.status(500).send('Error listing files.'); }); }); app.listen(3000, () => { console.log('Server is running on port 3000'); });
Conclusion
node7z provides a robust and simple way to manage archives within your Node.js applications. By utilizing the provided APIs, you can easily create, extract, and list contents of archive files.
Hash: 2f9f9de36770a0cf960db7af7d2571c7289bcb507b1c35572af21e7098942012