Home  >  Article  >  Web Front-end  >  nodejs下打包模块archiver详解_node.js

nodejs下打包模块archiver详解_node.js

WBOY
WBOYOriginal
2016-05-16 16:29:211733browse

archiver是一个在nodejs中能跨平台实现打包功能的模块,可以打zip和tar包,是一个比较好用的三方模块。

使用前先安装archiver模块。

复制代码 代码如下:

npm install archiver

建立一段代码

复制代码 代码如下:

var archiver = require('archiver');
var fs = require('fs');
//被打包文件
var files = [
  'files/001.png',
  'files/002.png'
  ];
var zipPath = 'test.zip';
//创建一最终打包文件的输出流
var output = fs.createWriteStream(zipPath);
//生成archiver对象,打包类型为zip
var zipArchiver = archiver('zip');
//将打包对象与输出流关联
zipArchiver.pipe(output);
for(var i=0; i   console.log(files[i]);
  //将被打包文件的流添加进archiver对象中
  zipArchiver.append(fs.createReadStream(files[i]), {'name': files[i]});
}
//打包
zipArchiver.finalize();

非常简单的完成打包功能。

本模块下载地址:https://github.com/ctalkington/node-archiver

Statement:
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