在Nodejs中使用archiver-zip-encrypted库加密压缩文件出现报错如何解决

发布时间:2021-03-23 15:22:56 作者:Leah
来源:亿速云 阅读:182

在Nodejs中使用archiver-zip-encrypted库加密压缩文件出现报错如何解决?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

在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。

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

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库加密压缩文件出现报错如何解决问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. java压缩文件后出现乱码怎么解决
  2. 在vue 中引入子组件出现报错如何解决

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

nodejs archiver-zip-encrypted

上一篇:怎么在android中利用surfaceview和MediaPlayer播放视频

下一篇:怎么在Python中使用filter去除列表中的空字符串

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》