Home > Web Front-end > Front-end Q&A > How to use archiver in nodejs

How to use archiver in nodejs

WBOY
Release: 2022-06-29 16:00:54
Original
2500 people have browsed it

In nodejs, archiver is used to compress and package some files into compressed packages in zip format or tar format; archiver is a module that can implement packaging functions across platforms. The packaging formats are zip and tar, which can be used The "npm install archiver" statement installs the module before use.

How to use archiver in nodejs

The operating environment of this article: Windows 10 system, nodejs version 12.19.0, Dell G3 computer.

How to use archiver in nodejs

Sometimes we need to compress and package some files into compressed packages in zip format or tar format, or we may also need to package the directory. In Node.js, you can use the third-party package archiver to perform operations.

archiver is a module that can realize cross-platform packaging function in nodejs. It can make zip and tar packages. It is a relatively easy-to-use third-party module.

Install the archive module before use.

The code is as follows:

npm install archiver
Copy after login

Introduction:

// 由于需要读取文件所以需要fs模块,也必须导入
const fs = require('fs');
const archiver = require('archiver');
Copy after login

Usage

The basic usage is as follows:

// 第一步,导入必要的模块
const fs = require('fs');
const archiver = require('archiver');

// 第二步,创建可写流来写入数据
const output = fs.createWriteStream(__dirname + "/hello.zip");// 将压缩包保存到当前项目的目录下,并且压缩包名为test.zip
const archive = archiver('zip', {zlib: {level: 9}});// 设置压缩等级

// 第三步,建立管道连接
archive.pipe(output);

// 第四步,压缩指定文件
var stream = fs.createReadStream(__dirname + "/hello.txt");// 读取当前目录下的hello.txt
archive.append(stream, {name: 'hello.txt'});

// 第五步,完成压缩
archive.finalize();
Copy after login

After the code is successfully executed, it will Generate a compressed package named hello.zip in the directory where the project is located, and the compressed package contains the compressed file hello.txt.
How to use archiver in nodejs

Example

Compress a single file

Compressed files can use archive.append() and archive.file () to perform operations.

The API for compressing a single file is as follows:

// 添加一个文件到压缩包,通过可写流的方式读取数据附加文件
const file1 = __dirname + '/file1.txt';
archive.append(fs.createReadStream(file1), { name: 'file1.txt' });

//添加一个文件到压缩包,通过将字符串写入到文件的方式附加文件
archive.append('string cheese!', { name: 'file2.txt' });

// 添加一个文件到压缩包,通过Buffer数据的方式附加文件
const buffer3 = Buffer.from('buff it!');
archive.append(buffer3, { name: 'file3.txt' });

// 添加一个文件到压缩包,直接传入文件路径
archive.file('file1.txt', { name: 'file4.txt' });
Copy after login

The complete example is as follows:

// 第一步,导入必要的模块
const fs = require('fs');
const archiver = require('archiver');

// 第二步,创建可写流来写入数据
const output = fs.createWriteStream(__dirname + "/hello.zip");// 将压缩包保存到当前项目的目录下,并且压缩包名为test.zip
const archive = archiver('zip', {zlib: {level: 9}});// 设置压缩等级

// 第三步,建立管道连接
archive.pipe(output);

// 第四步,压缩指定文件
archive.append(fs.createReadStream(__dirname + '/hello.txt'), {name: 'hello.txt'});// 文件流
archive.append('index.html', {name: 'index.html'});// 文件路径
archive.append(Buffer.from("这是Buffer格式的数据"), {name: 'buffer.txt'});// Buffer对象
archive.append("直接传入字符串", {name: 'string.txt'});// 字符串

// 第五步,完成压缩
archive.finalize();
Copy after login

How to use archiver in nodejs
Note: archive.append()The second parameter{name: 'hello.txt'} is to rename the corresponding file in the compressed package.

Compress multiple files

If you want to compress multiple files, just call the archive.append() method to append the files. These additional files will be added to in compressed package. For example:

// 第一步,导入必要的模块
const fs = require('fs');
const archiver = require('archiver');

// 第二步,创建可写流来写入数据
const output = fs.createWriteStream(__dirname + "/hello.zip");// 将压缩包保存到当前项目的目录下,并且压缩包名为test.zip
const archive = archiver('zip', {zlib: {level: 9}});// 设置压缩等级

// 第三步,建立管道连接
archive.pipe(output);

// 第四步,压缩多个文件到压缩包中
archive.append('index.html', {name: 'index.html'});
archive.append('hello.js', {name: 'hello.js'});
archive.append('hello.html', {name: 'hello.html'});
archive.append('db.json', {name: 'db.json'});

// 第五步,完成压缩
archive.finalize();
Copy after login

How to use archiver in nodejs

Compress directory

If you want to compress the directory, you need to use archive.directory() to complete it. The API is as follows:

// 将指定目录打包压缩到压缩包中,并且重命名为new-subdir,并且subdir目录下的所有文件仍然在new-subdir目录下,而不是在压缩包的根目录下
archive.directory('subdir/', 'new-subdir');

// 将指定目录下的所有文件打包压缩到压缩包中,而这些文件在压缩包的根目录,而非子目录中
archive.directory('subdir/', false);
Copy after login

The complete example is as follows:

// 第一步,导入必要的模块
const fs = require('fs');
const archiver = require('archiver');

// 第二步,创建可写流来写入数据
const output = fs.createWriteStream(__dirname + "/hello.zip");// 将压缩包保存到当前项目的目录下,并且压缩包名为test.zip
const archive = archiver('zip', {zlib: {level: 9}});// 设置压缩等级

// 第三步,建立管道连接
archive.pipe(output);

// 第四步,压缩目录到压缩包中
archive.directory('public/', 'new-public');
archive.directory('demo/', false);

// 第五步,完成压缩
archive.finalize();
Copy after login

How to use archiver in nodejs

Recommended learning: "nodejs video tutorial"

The above is the detailed content of How to use archiver in nodejs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template