美文网首页
nodejs 批量压缩文件或文件夹

nodejs 批量压缩文件或文件夹

作者: 化城 | 来源:发表于2017-06-10 02:17 被阅读676次

第一次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

如有问题,请留言。

相关文章

  • nodejs 批量压缩文件或文件夹

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

  • 一些shell

    批量压缩文件 for i in ls; do tar -zcf $i.tar.gz $i ; done 删除文件夹...

  • linux 压缩和解压缩命令

    压缩文件夹 查看压缩文件夹中的文件 解压文件夹

  • 自动删除node_modules

    使用nodejs之后,node_modules太多的问题 自动删除指定文件夹下的相关文件夹 如批量删除node_m...

  • Python批量压缩和移动文件

    一、压缩文件:使用zipfile和os 1、压缩单个文件 2、批量压缩 3、压缩整个文件夹 二、移动文件:使用sh...

  • Mac 命令行压缩、分割大文件

    压缩文件或者文件夹 命令行加密压缩文件、文件夹 示例 -P 指定密码 123 密码 -r 文件夹递归处理,压缩文件...

  • Linux中批量压缩文件夹

    'find ./* -type d' 表示查找所有文件夹,-type d就表示指定文件夹类型,这种方法是把每个文件...

  • 使用Matlab批量解压文件

    需求 批量解压大量gz压缩文件 代码

  • mac加密压缩

    mac中利用终端加密压缩文件/文件夹: 首先打开终端, 找到文件夹目录 cd 路径 : 使用命令压缩 压缩文件夹 ...

  • Shell编程--case,for,while,until

    shell多分支case语句 for循环 语法一: 批量压缩文件脚本 语法二: 从1加到100 批量添加用户 批量...

网友评论

      本文标题:nodejs 批量压缩文件或文件夹

      本文链接:https://www.haomeiwen.com/subject/nxrqqxtx.html