Nodejs使用archiver-zip-encrypted库加密压缩文件时报错(解决方案)
前几天在维护一个nodejs写的命令行工具,要增加一个压缩zip文件时加密码功能。压缩文件时使用了 archiver库,加密码使用了 archiver-zip-encrypted库。在windows系统上测试时,发现会概率的出现以下异常:
events.js:174 thrower;//Unhandled'error'event ^ Error:filedatastreamhasunexpectednumberofbytes atByteCounter.( xxx\node_modules\yazl\index.js:162:99) atByteCounter.emit(events.js:194:15) atendReadableNT(_stream_readable.js:1103:12) atprocess._tickCallback(internal/process/next_tick.js:63:19) Emitted'error'eventat: atByteCounter.(xxx\node_modules\yazl\index.js:162:85) atByteCounter.emit(events.js:194:15) atendReadableNT(_stream_readable.js:1103:12) atprocess._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文件中发生异常,找到出现异常的代码如下所示:
functionpumpFileDataReadStream(self,entry,readStream){
varcrc32Watcher=newCrc32Watcher();
varuncompressedSizeCounter=newByteCounter();
varcompressor=entry.compress?newzlib.DeflateRaw():newPassThrough();
varcompressedSizeCounter=newByteCounter();
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)returnself.emit("error",newError("filedatastreamhasunexpectednumberofbytes"));
}
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){
varself=this;
metadataPath=validateMetadataPath(metadataPath,false);
if(options==null)options={};
varentry=newEntry(metadataPath,false,options);
self.entries.push(entry);
fs.stat(realPath,function(err,stats){
if(err)returnself.emit("error",err);
if(!stats.isFile())returnself.emit("error",newError("notafile:"+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在这里创建的
varreadStream=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("entrysize:"+entry.uncompressedSize+",uncompressedSize:"+uncompressedSizeCounter.byteCount);
if(entry.uncompressedSize!==uncompressedSizeCounter.byteCount)returnself.emit("error",newError("filedatastreamhasunexpectednumberofbytes"));
}
在 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库加密压缩文件时报错,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。