nodejs 批量压缩文件或文件夹

第一次node js循环使用,开始的时候还以为要等待第一个压缩完成后循环第二个,结果我错了。
异步原来是这样。

牛!120文件手动压缩,估计要好久。程序太方便了,10秒就压缩完了。
不过自己写程序用了1个多小时。下次压缩文件肯定可以用的到。

/**
 * Created by Administrator on 2017/6/9.
 */
var fs = require('fs');
var archiver = require('archiver');

var path=__dirname;
var dirList = fs.readdirSync(path);
var status=true;
dirList.forEach(function(item){

    console.log(item);
// create a file to stream archive data to.
var output = fs.createWriteStream('F:\\work\\wxapp\\input\\'+item+'.zip');
var archive = archiver('zip', {
    zlib: { level: 9 } // Sets the compression level.
});

// listen for all archive data to be written
output.on('close', function() {
    console.log(archive.pointer()/1024/1024 + 'M');
    console.log('压缩完成');
});

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

// pipe archive data to the file

        archive.pipe(output);



archive.directory(item+'/');
archive.finalize();

});


// append a file from stream
/*var file1 = __dirname + '/120个小程序源码/AppleMusic';
archive.append(fs.createReadStream(file1), { name: 'AppleMusic' });*/

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

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

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

// append files from a directory
archive.directory('subdir/');

// append files from a glob pattern
archive.glob('subdir/!*.txt');*/

// finalize the archive (ie we are done appending files but streams have to finish yet)
//archive.finalize();

参考文档:
https://github.com/archiverjs/node-archiver
http://blog.csdn.net/hero82748274/article/details/45700465

如有问题,请留言。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,322评论 25 709
  • 在此特此声明:一下所有链接均来自互联网,在此记录下我的查阅学习历程,感谢各位原创作者的无私奉献 ! 技术一点一点积...
    远航的移动开发历程阅读 13,870评论 12 197
  • 呆在家里的昏睡情况就是,无缘无故也选择了睡觉。 但是每次醒来都不昏迷的感觉。 可能是我之前有喝中药补肾,也许是因为...
    jewelduan阅读 1,820评论 0 1
  • 引言:世人常以“美若天仙”四字形容女子之美,但天仙究竟如何美法,谁也不知,此时一见那少女,各人心头都不自禁的涌出“...
    吃盼君阅读 4,636评论 7 6
  • 题目描述:给正整数 n 表示楼梯级数,每次能迈一层或者两层,问一共有多少种登顶法。 分析:基础的常见题,两种思路三...
    Nautilus1阅读 780评论 0 1