【问题标题】:Compressing multiple files using zlib使用 zlib 压缩多个文件
【发布时间】:2014-03-25 18:29:55
【问题描述】:

以下代码将压缩一个文件。如何压缩多个文件

var gzip = zlib.createGzip();
var fs = require('fs');
var inp = fs.createReadStream('input.txt');
var out = fs.createWriteStream('input.txt.gz');

inp.pipe(gzip).pipe(out);

【问题讨论】:

  • 如果要压缩整个目录结构,请尝试使用递归函数获取目录和文件的名称。您可以使用其他模块,但这是理论。
  • 为什么要投反对票,这个问题是完全合理的(因为 node.js 文档根本没有提到它)

标签: node.js compression gzip zlib


【解决方案1】:

你可以使用的东西:

var listOfFiles = ['firstFile.txt', 'secondFile.txt', 'thirdFile.txt'];

function compressFile(filename, callback) {
    var compress = zlib.createGzip(),
        input = fs.createReadStream(filename),
        output = fs.createWriteStream(filename + '.gz');

    input.pipe(compress).pipe(output);

    if (callback) {
        output.on('end', callback);
    }
}

#1 方法:

// Dummy, just compress the files, no control at all;
listOfFiles.forEach(compressFile);

#2方法:

// Compress the files in waterfall and run a function in the end if it exists
function getNext(callback) {
    if (listOfFiles.length) {
        compressFile(listOfFiles.shift(), function () {
            getNext(callback);
        });
    } else if (callback) {
        callback();
    }
}

getNext(function () {
    console.log('File compression ended');
});

【讨论】:

  • 我想要一个包含所有文件的单个 zip 文件。
  • zip 文件的文件格式比仅仅连接文件en.wikipedia.org/wiki/Zip_%28file_format%29 更具体,让我为你 npm 这个? npmjs.org/package/zip-paths
  • 我有点困惑为什么这个答案被接受了,因为它似乎没有回答这个问题(根据上面 user3180402 的评论 ^)
  • 他只是添加了一个循环,没有任何连接。
【解决方案2】:

Gzip 是一种压缩一串数据的算法。它对文件或文件夹一无所知,因此不能自己做你想做的事。您可以使用归档工具构建单个归档文件,然后使用 gzip 压缩构成归档的数据:

有关更多信息,另请参阅此答案:Node.js - Zip/Unzip a folder

【讨论】:

    【解决方案3】:

    最好的包(也是唯一一个仍然维护和正确记录的包)似乎是archiver

    var fs = require('fs');
    var archiver = require('archiver');
    var output = fs.createWriteStream('./example.tar.gz');
    var archive = archiver('tar', {
        gzip: true,
        zlib: { level: 9 } // Sets the compression level.
    });
    
    archive.on('error', function(err) {
      throw err;
    });
    
    // pipe archive data to the output file
    archive.pipe(output);
    
    // append files
    archive.file('/path/to/file0.txt', {name: 'file0-or-change-this-whatever.txt'});
    archive.file('/path/to/README.md', {name: 'foobar.md'});
    
    // Wait for streams to complete
    archive.finalize();
    

    【讨论】:

    • 这可以完美地完成这项工作。
    【解决方案4】:
    module.exports = class ZlibModule{
    
    listOfImages = ['a.png','b.jpg'];
    fs = require('fs')
    zlib = require('zlib');
    
    constructor(req,res){
        this.req = req;
        this.res = res;
    }
    
    compressOperation(){
        this.listOfImages.forEach((value,index) => {
            const readStream = this.fs.createReadStream(rootDirectory+'/modules/zlibModule/images/'+value);
            const writeStream = this.fs.createWriteStream(rootDirectory+'/modules/zlibModule/'+value+'.gz');
            const compress = this.zlib.createGzip()
            readStream
                .on('error',(error)=>{
                    this.res.end(`${error.path} path does't exist`)
                })
                .pipe(compress)
                .pipe(writeStream)
                .on('error',(error)=>{
                    console.log(error);
                    this.res.end(`Something went to wrong`)
                })
                .on('finish',()=>{
                    if(index+1 === this.listOfImages.length) this.res.end("Done");
                })
        });
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多