JSZip内存问题

我在Node.js应用程序中遇到了很大的内存消耗,当一个接一个地加载〜100MB zip文件时,它将它们作为“NodeBufferReader”保存在内存中。 我正在使用的库称为JSZip,可以在这里find: https ://stuk.github.io/jszip/

如果我访问相同的zip文件两次,那么它不会增加内存使用量,但是对于每个“额外的”.zip文件,我访问内存的时间大约会增加.zip文件的大小。 我所访问的文件大概都在100MB或更大,所以你可以想象这有可能变得相当大,相当快。

Node.js应用程序是一个websocket服务器,它从.zip文件中读取文件并将其作为base64数据返回给请求者。 有问题的function在这里:

function handleFileRequest(args, connection_id) { var zipIndex = 0, pathLen = 0, zip_file = "", zip_subdir = ""; try { if (args.custom.file.indexOf(".zip") > -1) { // We have a .zip directory! zipIndex = args.custom.file.indexOf(".zip") + 4; pathLen = args.custom.file.length; zip_file = args.custom.file.substring(0, zipIndex); zip_subdir = args.custom.file.substring(zipIndex + 1, pathLen); fs.readFile(zip_file, function (err, data) { if (!err) { zipObj.load(data); if (zipObj.file(zip_subdir)) { var binary = zipObj.file(zip_subdir).asBinary(); var base64data = btoa(binary); var extension = args.custom.file.split('.').pop(); var b64Header = "data:" + MIME[extension] + ";base64,"; var tag2 = args.custom.tag2 || "unset"; var tag3 = args.custom.tag3 || "unset"; var rargs = { action: "getFile", tag: args.tag, dialogName: connections[connection_id].dialogName, custom: { file: b64Header + base64data, tag2: tag2, tag3: tag3 } }; connections[connection_id].sendUTF(JSON.stringify(rargs)); rargs = null; binary = null; base64data = null; } else { serverLog(connection_id, "Requested file doesn't exist"); } } else { serverLog(connection_id, "There was an error retrieving the zip file data"); } }); } else { // File isn't a .zip } } catch (e) { serverLog(connection_id, e); } } 

内存问题

任何帮助将非常感谢摆脱这个问题 – 谢谢!

工作代码示例

 function handleFileRequest(args, connection_id) { var zipIndex = 0, pathLen = 0, f = "", d = ""; try { if (args.custom.file.indexOf(".zip") > -1) { // We have a .zip directory! zipIndex = args.custom.file.indexOf(".zip") + 4; pathLen = args.custom.file.length; f = args.custom.file.substring(0, zipIndex); d = args.custom.file.substring(zipIndex + 1, pathLen); fs.readFile(f, function (err, data) { var rargs = null, binary = null, base64data = null, zipObj = null; if (!err) { zipObj = new JSZip(); zipObj.load(data); if (zipObj.file(d)) { binary = zipObj.file(d).asBinary(); base64data = btoa(binary); var extension = args.custom.file.split('.').pop(); var b64Header = "data:" + MIME[extension] + ";base64,"; var tag2 = args.custom.tag2 || "unset"; var tag3 = args.custom.tag3 || "unset"; rargs = { action: "getFile", tag: args.tag, dialogName: connections[connection_id].dialogName, custom: { file: b64Header + base64data, tag2: tag2, tag3: tag3 } }; connections[connection_id].sendUTF(JSON.stringify(rargs)); } else { serverLog(connection_id, "Requested file doesn't exist"); } } else { serverLog(connection_id, "There was an error retrieving the zip file data"); } rargs = null; binary = null; base64data = null; zipObj = null; }); } else { // Non-Zip file } } catch (e) { serverLog(connection_id, e); } } 

如果使用相同的JSZip实例来加载每个文件,则会将所有内容保留在内存中: load方法不会replace现有的内容。

每次尝试使用新的JSZip实例:

 var zipObj = new JSZip(); zipObj.load(data); // or var zipObj = new JSZip(data);