国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

Nodejs使用archiver-zip-encrypted庫加密壓縮文件時報錯(解決方案)

2024-05-06 15:43:53
字體:
來源:轉載
供稿:網友

前幾天在維護一個nodejs寫的命令行工具,要增加一個壓縮zip文件時加密碼功能。壓縮文件時使用了 archiver 庫,加密碼使用了 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()

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 吕梁市| 嘉义县| 卢龙县| 莱芜市| 伊宁县| 海阳市| 福州市| 阳高县| 宜城市| 石柱| 永宁县| 罗山县| 忻州市| 平舆县| 美姑县| 改则县| 桃园市| 江永县| 大田县| 司法| 天等县| 永安市| 华容县| 巩留县| 建宁县| 大余县| 丽水市| 新田县| 平顺县| 广平县| 太谷县| 甘泉县| 南丹县| 甘洛县| 寿宁县| 青浦区| 巴中市| 兴城市| 珠海市| 登封市| 武鸣县|