Node.js: Gzip compression?

前端 未结 13 1363
迷失自我
迷失自我 2020-11-30 21:57

Am I wrong in finding that Node.js does no gzip compression and there are no modules out there to perform gzip compression? How can anyone use a web server that has no compr

相关标签:
13条回答
  • 2020-11-30 22:57

    For compressing the file you can use below code

    var fs = require("fs");
    var zlib = require('zlib');
    fs.createReadStream('input.txt').pipe(zlib.createGzip())
    .pipe(fs.createWriteStream('input.txt.gz'));
    console.log("File Compressed.");
    

    For decompressing the same file you can use below code

    var fs = require("fs");
    var zlib = require('zlib');
    fs.createReadStream('input.txt.gz')
    .pipe(zlib.createGunzip())
    .pipe(fs.createWriteStream('input.txt'));
    console.log("File Decompressed.");
    
    0 讨论(0)
提交回复
热议问题