gpt4 book ai didi

javascript - JSZip 内存问题

转载 作者:太空宇宙 更新时间:2023-11-03 23:38:40 25 4
gpt4 key购买 nike

我的 Node.js 应用程序内存消耗很高,当一个接一个地加载 ~100MB zip 文件时,它会将它们作为“NodeBufferReader”保留在内存中。我正在使用的库称为 JSZip,可在此处找到:https://stuk.github.io/jszip/

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

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

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);
}
}

memory problem

任何帮助解决这个问题的帮助将不胜感激 - 谢谢!

工作代码示例

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 方法不会替换现有内容。

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

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

关于javascript - JSZip 内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27693898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com