首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSZip内存问题

JSZip内存问题
EN

Stack Overflow用户
提问于 2014-12-29 18:13:02
回答 1查看 2.2K关注 0票数 1

在我的Node.js应用程序上,我的内存消耗很大,当一个接一个地加载~100 my的压缩文件时,它会将它们作为一个"NodeBufferReader“保存在内存中。我使用的库名为JSZip,在这里可以找到:https://stuk.github.io/jszip/

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

Node.js应用程序是一个websocket服务器,它从.zip文件中读取文件并将它们作为base64数据返回给请求者。有关职能如下:

代码语言:javascript
复制
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);
    }
}

任何帮助都将是非常感谢的解决这个问题-谢谢!

工作代码示例

代码语言:javascript
复制
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);
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-29 20:41:42

如果您使用同一个JSZip实例来加载每个文件,那么您将将所有内容保存在内存中:load方法不会替换现有的内容。

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

代码语言:javascript
复制
var zipObj = new JSZip();
zipObj.load(data);
// or var zipObj = new JSZip(data);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27693898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档