Home  >  Article  >  Web Front-end  >  How to create and extract zip files in Node.js? Method introduction

How to create and extract zip files in Node.js? Method introduction

青灯夜游
青灯夜游forward
2020-11-26 17:54:304833browse

How to create and extract zip files in Node.js? Method introduction

Related recommendations: "nodejs Tutorial"

Zip file is a commonly used compressed file format. Most developers end up using tarballs instead of zip files. But there are some situations where you need to use zip files, such as uploading functions to AWS Lambda. In this article, I will demonstrate how to create and extract zip files using the adm-zip npm module.

Create a Zip file

Export an AdmZip class from the npm module of adm-zip. An instance of AdmZip corresponds to a zip file. If you want to create a new zip file, you should call new AdmZip() without any parameters:

const AdmZip = require('adm-zip');

const file = new AdmZip();

There are many ways to add files and directories to file. Files and directories can be added from the file system by file name using file.addLocalFile() and file.addLocalFolder(). For example, the following code demonstrates how to add the package.json file and the node_modules directory to the zip file:

const AdmZip = require('adm-zip');

const file = new AdmZip();

file.addLocalFile('./package.json');
//第二个node_modules 参数是 zip 中目录的路径。
//如果没有第二个参数,则`./node_modules`中的每个目录都将是压缩文件中的顶级目录
file.addLocalFolder('./node_modules', 'node_modules');

Then, you can use the following two methods A write file:

//写入zip文件的第一种方法:将其转换为缓冲区并使用`fs`
const fs = require('fs');

fs.writeFileSync('output.zip', file.toBuffer());

// 另一种写入 zip 文件的方法:writeZip()
file.writeZip('output.zip');

After writing the output.zip file, you should be able to open it in your custom zip decompression program. Below is the file opened in Xubuntu's default archive manager engrampa.

How to create and extract zip files in Node.js? Method introduction

addLocalFile() and addLocalFolder() The second parameter is the path to put the file or directory contents into the zip . For example, if you want to place both package.json and node_modules in the project directory, you can run the following script:

const AdmZip = require('adm-zip');

const file = new AdmZip();

file.addLocalFile('./package.json', 'project');
file.addLocalFolder('./node_modules', 'project/node_modules');

const fs = require('fs');

fs.writeFileSync('output.zip', file.toBuffer());

can be used file.addFile() Method adds a file from the original Node.js buffer. Here's how to add a text file containing the string Hello, World to a zip file without creating a file on the file system.

const AdmZip = require('adm-zip');

const file = new AdmZip();

file.addFile('hello.txt', Buffer.fromString('Hello, World'));

const fs = require('fs');

fs.writeFileSync('output.zip', file.toBuffer());

Use an existing file

If you pass parameters to the AdmZip constructor, adm-zip will parse the file at the given path. Here's how to extract everything from the output.zip file to the directory output.

const AdmZip = require('adm-zip');

const file = new AdmZip('./output.zip');

file.extractAllTo('./output');

You can also use file.extractEntryTo() to extract a single file from a zip file. For example, here's how to pull the hello.txt file from a zip file and write it to the current directory:

const AdmZip = require('adm-zip');

const file = new AdmZip('./output.zip');

file.extractEntryTo('hello.txt', './');

You can also use addLocalFile( ), addLocalFolder() and addFile().

Summary

zip files are often used for compression. Some services, such as AWS Lambda, require you to use zip files. Fortunately, the adm-zip npm module makes it easy to create and extract zip files directly from Node.js.

English original address: http://thecodebarbarian.com/working-with-zip-files-in-node-js.html

Author: Valeri Karpov

For more programming-related knowledge, please visit: Programming Video! !

The above is the detailed content of How to create and extract zip files in Node.js? Method introduction. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete