用户登录
用户注册

分享至

Nodejs使用archiver-zip-encrypted库加密压缩文件时报错(解决方案)

  • 作者: 花果山梁山泊荆州牧红孩儿
  • 来源: 51数据库
  • 2021-08-23

前几天在维护一个nodejs写的命令行工具,要增加一个压缩zip文件时加密码功能。压缩文件时使用了 库,加密码使用了 库。在windows系统上测试时,发现会概率的出现以下异常:

events.js:174 
throw er; // unhandled 'error' event 
^
error: file data stream has unexpected number of bytes 
at bytecounter. (
xxx\node_modules\yazl\index.js:162:99) 
at bytecounter.emit (events.js:194:15) 
at endreadablent (_stream_readable.js:1103:12) 
at process._tickcallback (internal/process/next_tick.js:63:19) 
emitted 'error' event at: 
at bytecounter. (xxx\node_modules\yazl\index.js:162:85) 
at bytecounter.emit (events.js:194:15) 
at endreadablent (_stream_readable.js:1103:12) 
at process._tickcallback (internal/process/next_t

我的本机环境是:

npm:6.9.0
node: v10.16.3

在另外一个同事的windows系统上测试,他那边是上面异常必现,对应的node版本是v10.15。

具体使用的代码不贴了,基本上是参照官方来写的,压缩完成最后调用代码如下所示:

archive.finalize().then(() => {
  // 到这里认为是压缩完成,进行后续处理,实际并没有,参照后面分析
  anotherprocess();
}).catch(err => {
  // 压缩出现异常处理...
});

出现异常后一一检查代码和官方demo不一样的地方,并没有发现什么异常之处,网上搜索也没有发现这种异常记录。由于刚接触js,不是很熟,就从问题开始下手,找到出现问题的代码,开始调试。

错误日志中提示是在 yzal/index.js 文件中发生异常,找到出现异常的代码如下所示:

function pumpfiledatareadstream(self, entry, readstream) {
 var crc32watcher = new crc32watcher();
 var uncompressedsizecounter = new bytecounter();
 var compressor = entry.compress ? new zlib.deflateraw() : new passthrough();
 var compressedsizecounter = new bytecounter();
 readstream.pipe(crc32watcher)
      .pipe(uncompressedsizecounter)
      .pipe(compressor)
      .pipe(compressedsizecounter)
      .pipe(self.outputstream, {end: false});
 compressedsizecounter.on("end", function() {
  entry.crc32 = crc32watcher.crc32;
  if (entry.uncompressedsize == null) {
   entry.uncompressedsize = uncompressedsizecounter.bytecount;
  } else {
   // 异常从这里抛出来的
   if (entry.uncompressedsize !== uncompressedsizecounter.bytecount) return self.emit("error", new error("file data stream has unexpected number of bytes"));
  }
  entry.compressedsize = compressedsizecounter.bytecount;
  self.outputstreamcursor += entry.compressedsize;
  writetooutputstream(self, entry.getdatadescriptor());
  entry.state = entry.file_data_done;
  pumpentries(self);
 });
}

从上面代码可以看出来: uncompressedsizecounter.bytecount 是从 pumpfiledatareadstream() 函数 readstream 参数中获取的属性值,而 entry.uncompressedsize 也是函数的 entry 参数中获取的属性。接着找 pumpfiledatareadstream() 函数是从哪里调用的。

通过输出日志得出 pumpfiledatareadstream() 函数是在以下面的代码中被调用的:

zipfile.prototype.addfile = function(realpath, metadatapath, options) {
 var self = this;
 metadatapath = validatemetadatapath(metadatapath, false);
 if (options == null) options = {};
 var entry = new entry(metadatapath, false, options);
 self.entries.push(entry);
 fs.stat(realpath, function(err, stats) {
  if (err) return self.emit("error", err);
  if (!stats.isfile()) return self.emit("error", new error("not a file: " + realpath));
  // 这里是文件的大小
  entry.uncompressedsize = stats.size;
  if (options.mtime == null) entry.setlastmoddate(stats.mtime);
  if (options.mode == null) entry.setfileattributesmode(stats.mode);
  entry.setfiledatapumpfunction(function() {
   // readstream在这里创建的
   var readstream = fs.createreadstream(realpath);
   entry.state = entry.file_data_in_progress;
   readstream.on("error", function(err) {
    self.emit("error", err);
   });
   // 在这里被调用
   pumpfiledatareadstream(self, entry, readstream);
  });
  pumpentries(self);
 });
};

从上面代码可以看出来 entry.uncompressedsize 是stats.size,即文件的大小, readstream 是创建的文件流。但是在什么情况下两者会不一样呢?感觉只可能在文件还没有读取完,但是是什么原因导致这种情况发生?由于对js接触的时间不长,没有进行深入分析。最后在抛出异常的上面一行用 console.log 将两个属性的大小值都输出,代码如下所示:

if (entry.uncompressedsize == null) {
   entry.uncompressedsize = uncompressedsizecounter.bytecount;
} else {
 // 增加日志输出
 console.log("entry size: " + entry.uncompressedsize + ", uncompressedsize: " + uncompressedsizecounter.bytecount);
 if (entry.uncompressedsize !== uncompressedsizecounter.bytecount) return self.emit("error", new error("file data stream has unexpected number of bytes"));
}

在 archive.finalize() 时和 output 写入流的 close 事件时(详细参照官方的示例代码),分别加上日志输出,代码如下所示:

archive.finalize().then(() => {
 // 到这里认为是压缩完成,进行后续处理,实际并没有,参照后面分析
 console.log("finalize");
 // anotherprocess();
}).catch(err => {
  // 压缩出现异常
});
output.on('close', function() {
 console.log('close');
 // 这个业务函数与上面finalize函数中的是互斥,不会同时存在
 anotherprocess();
});

最后分别将 anotherprocess() 函数加到两个异步回调中执行,发现在 close 事件执行时,两个size输出的大小一致,都是文件的大小。而在 finalize 场景测试发现 uncompressedsize 要小于文件的大小。最后将 anotherprocess() 函数放在 close 事件回调函数中执行,问题解决。

总结

以上所述是小编给大家介绍的nodejs使用archiver-zip-encrypted库加密压缩文件时报错,希望对大家有所帮助

软件
前端设计
程序设计
Java相关