zoukankan      html  css  js  c++  java
  • nodejs 批量压缩文件或文件夹

    /**
     * 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();



  • 相关阅读:
    tesseract的简单使用
    快速添加请求头
    1010. 一元多项式求导 (25)
    1009. 说反话 (20)
    1008. 数组元素循环右移问题 (20)
    1007. 素数对猜想 (20)
    1006. 换个格式输出整数 (15)
    素数判断
    1002. 写出这个数 (20)
    1005. 继续(3n+1)猜想 (25)
  • 原文地址:https://www.cnblogs.com/luoguixin/p/12432761.html
Copyright © 2011-2022 走看看