在c#.net中更新zip时出现内存不足异常 [英] Out of memory exception while updating zip in c#.net

查看:98
本文介绍了在c#.net中更新zip时出现内存不足异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试在c#.net的Zip文件中添加文件时遇到OutofMemoryException.我正在使用32位体系结构来构建和运行应用程序

I am getting OutofMemoryException While trying to add files in Zip file in c#.net. I am using 32bit architecture for bulding and running application

            string[] filePaths = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\capture\\capture");
            System.IO.Compression.ZipArchive zip = ZipFile.Open(filePaths1[c], ZipArchiveMode.Update);

                foreach (String filePath in filePaths)

                {
                    string nm = Path.GetFileName(filePath);
                    zip.CreateEntryFromFile(filePath, "capture/" + nm, CompressionLevel.Optimal);
                }
                zip.Dispose();
                zip = null;

我无法理解背后的共鸣

推荐答案

确切原因取决于多种因素,但很可能您只是在存档中添加了太多内容.尝试改用ZipArchiveMode.Create选项,该选项将存档直接写入磁盘,而不将其缓存在内存中.

The exact reason depends on a variety of factors, but most likely you are simply just adding too much to the archive. Try using the ZipArchiveMode.Create option instead, which writes the archive directly to disk without caching it in memory.

如果您确实要更新现有存档,则仍可以使用ZipArchiveMode.Create.但是,这将需要打开现有档案,将其所有内容复制到新档案(使用Create),然后添加新内容.

If you are really trying to update an existing archive, you can still use ZipArchiveMode.Create. But it will require opening the existing archive, copying all of its contents to a new archive (using Create), and then adding the new content.

没有好的,最小完整代码示例,无法确定该异常来自何处,更不用说如何解决它了.

Without a good, minimal, complete code example, it would not be possible to say for sure where the exception is coming from, never mind how to fix it.

我的意思是打开现有档案,将其所有内容复制到新档案(使用Create),然后添加新内容":

Here is what I mean by "…opening the existing archive, copying all of its contents to a new archive (using Create), and then adding the new content":

string[] filePaths = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\capture\\capture");

using (ZipArchive zipFrom = ZipFile.Open(filePaths1[c], ZipArchiveMode.Read))
using (ZipArchive zipTo = ZipFile.Open(filePaths1[c] + ".tmp", ZipArchiveMode.Create))
{
    foreach (ZipArchiveEntry entryFrom in zipFrom.Entries)
    {
        ZipArchiveEntry entryTo = zipTo.CreateEntry(entryFrom.FullName);

        using (Stream streamFrom = entryFrom.Open())
        using (Stream streamTo = entryTo.Open())
        {
            streamFrom.CopyTo(streamTo);
        }
    }

    foreach (String filePath in filePaths)
    {
        string nm = Path.GetFileName(filePath);
        zipTo.CreateEntryFromFile(filePath, "capture/" + nm, CompressionLevel.Optimal);
    }
}

File.Delete(filePaths1[c]);
File.Move(filePaths1[c] + ".tmp", filePaths1[c]);

或者类似的东西.缺少良好的,最小的,完整的代码示例,我只是在浏览器中编写了以上代码.我没有尝试编译它,没关系测试它.您可能需要调整一些细节(例如,临时文件的处理).但希望您能明白.

Or something like that. Lacking a good, minimal, complete code example, I just wrote the above in my browser. I didn't try to compile it, never mind test it. And you may want to adjust some specifics (e.g. the handling of the temp file). But hopefully you get the idea.

这篇关于在c#.net中更新zip时出现内存不足异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆