Node.js包之archiver压缩打包文件或目录为zip格式

概述

有时候我们需要将一些文件压缩打包成zip格式或tar格式的压缩包,也有可能需要将目录进行打包。在Node.js中就可以用到archiver这个第三方包来进行操作。

archiver官网

安装

安装:

npm install archiver --save

引入:

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

使用

基本使用如下:

// 第一步,导入必要的模块
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();

执行代码成功后,就会在项目的所在目录下生成一个名为hello.zip压缩包,该压缩包内就是压缩的文件hello.txt。
在这里插入图片描述

实例

压缩单个文件

压缩文件可以使用archive.append()archive.file()来进行操作。

压缩单个文件的API如下:

// 添加一个文件到压缩包,通过可写流的方式读取数据附加文件
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' });

完整的例子如下:

// 第一步,导入必要的模块
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();

在这里插入图片描述
注意:archive.append()第二个参数{name: 'hello.txt'}是对压缩包中对应的文件进行重命名。

压缩多个文件

如果要压缩多个文件,直接调用archive.append()方法附加文件即可,这些附加的文件都会被添加到压缩包中。如例:

// 第一步,导入必要的模块
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();

在这里插入图片描述

压缩目录

如果要压缩目录,则需要使用archive.directory()来完成。API如下:

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

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

完整实例如下:

// 第一步,导入必要的模块
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();

在这里插入图片描述

压缩文件和目录

可以同时压缩文件和目录,只需要使用archive.append()archive.directory()方法附加文件和目录到压缩包就可以了。

// 第一步,导入必要的模块
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('abc', {name: 'abc.txt'});
archive.file('hello.txt', {name: 'new-hello.txt'});
archive.directory('public/', 'new-public');

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

在这里插入图片描述

其他

官方API

// require modules
const fs = require('fs');
const archiver = require('archiver');

// create a file to stream archive data to.
const output = fs.createWriteStream(__dirname + '/example.zip');
const archive = archiver('zip', {
  zlib: { level: 9 } // Sets the compression level.
});

// listen for all archive data to be written
// 'close' event is fired only when a file descriptor is involved
output.on('close', function() {
  console.log(archive.pointer() + ' total bytes');
  console.log('archiver has been finalized and the output file descriptor has closed.');
});

// This event is fired when the data source is drained no matter what was the data source.
// It is not part of this library but rather from the NodeJS Stream API.
// @see: https://nodejs.org/api/stream.html#stream_event_end
output.on('end', function() {
  console.log('Data has been drained');
});

// good practice to catch warnings (ie stat failures and other non-blocking errors)
archive.on('warning', function(err) {
  if (err.code === 'ENOENT') {
    // log warning
  } else {
    // throw error
    throw err;
  }
});

// good practice to catch this error explicitly
archive.on('error', function(err) {
  throw err;
});

// pipe archive data to the file
archive.pipe(output);

// append a file from stream
const file1 = __dirname + '/file1.txt';
archive.append(fs.createReadStream(file1), { name: 'file1.txt' });

// append a file from string
archive.append('string cheese!', { name: 'file2.txt' });

// append a file from buffer
const buffer3 = Buffer.from('buff it!');
archive.append(buffer3, { name: 'file3.txt' });

// append a file
archive.file('file1.txt', { name: 'file4.txt' });

// append files from a sub-directory and naming it `new-subdir` within the archive
archive.directory('subdir/', 'new-subdir');

// append files from a sub-directory, putting its contents at the root of archive
archive.directory('subdir/', false);

// append files from a glob pattern
archive.glob('file*.txt', {cwd:__dirname});

// finalize the archive (ie we are done appending files but streams have to finish yet)
// 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand
archive.finalize();
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值