更新zip时出现内存不足异常 [英] Out of memory exception while updating zip

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

问题描述

我在尝试将文件添加到.zip文件时收到 OutofMemoryException .我正在使用32位体系结构来构建和运行该应用程序.

I am getting OutofMemoryException while trying to add files to a .zip file. I am using 32-bit architecture for building and running the 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;

我无法理解其背后的原因.

I am unable to understand the reason behind it.

推荐答案

确切原因取决于多种因素,但很可能您只是在存档中添加了太多内容.尝试改用 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.

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

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