Node.js-Node.js存档器和fs stat上文件大小的短缺 [英] Nodejs - Shortage of file size on nodejs archiver and fs stat

查看:106
本文介绍了Node.js-Node.js存档器和fs stat上文件大小的短缺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序的逻辑如下:

  1. 从AWS s3获取对象
  2. 压缩所有从s3获得的对象(我创建了一个名为a.zip的zip文件)
  3. 将z.aip文件放入s3

现在我遇到一个问题,我的a.zip文件总是缺少几个字节.从以下代码段中,console.log(archive.pointer())和console.log(stat.size)的结果将有所不同.有人有主意吗?谢谢.

Now I face a problem that my a.zip file always lack of a few bytes. From the following code snippet, the result of console.log(archive.pointer()) and console.log(stat.size) will be different. Does anyone have an idea ? Thank you.

async.map(filename, util.getS3Obj, function(err, result) {
   var archiver = require('archiver');
   var archive = archiver.create('zip', {});
   archive.pipe(fs.createWriteStream('./tmp/Archive.zip'));
   for(var i=0 ; i<result.length ; i++) {
      archive.append(result[i], {name: fileNameArr[i]});
   }
   archive.finalize();

   archive.on('end', function() {
     console.log(archive.pointer());
     fs.stat('./tmp/Archive.zip', function(err, stat) {
        if(err) {}
        console.log(stat.size);    
      }); 
   });
})

推荐答案

我通过在createWriteStream的close事件中检查文件大小而不使用档案的结束事件来解决此问题.因为在归档的最后事件中,writeFileStream尚未完成.

I solve the problem by checking the file size in the close event of the createWriteStream without using end event of the archive. Because in the end event of archive, the writeFileStream is not finish.

var out = fs.createWriteStream('Archive.zip');
out.on('close', function() {
    // check file size here ...
});

这篇关于Node.js-Node.js存档器和fs stat上文件大小的短缺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆