创建 ziparchive native c# out of memory memorystream [英] Create ziparchive native c# out of memory memorystream

查看:28
本文介绍了创建 ziparchive native c# out of memory memorystream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用内置功能制作 zip system.io.compression.ziparchive

I am using the build in feature to make a zip system.io.compression.ziparchive

https://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive.getentry%28v=vs.110%29.aspx

并得到:

mscorlib.dll 中出现类型为System.OutOfMemoryException"的异常,但未在用户代码中处理

An exception of type 'System.OutOfMemoryException' occurred in mscorlib.dll but was not handled in user code

我读过这个:ZipArchive 创建无效的 ZIP 文件这是我在 *.ashx 文件(网络表单)中的一些代码:

I have read this: ZipArchive creates invalid ZIP file and this is some of my code in an *.ashx file (webforms):

Dictionary<string, string> csvs = new Dictionary<string, string>();
using (var memoryStream = new MemoryStream())
{
    using (var zip = new ZipArchive(memoryStream, ZipArchiveMode.Update, true))
    {
        // start loop for 2000 products
        zip.CreateEntryFromFile(prodImg, brandName + @"" + dr["ProductPictureName"]);
        // per product CreateEntryFromFile * 4

        // more lines
        csvs[tmpName] = value + sb.ToString();
    }
}
memoryStream.Seek(0, SeekOrigin.Begin);
context.Response.BinaryWrite(memoryStream.ToArray());
context.Response.Flush();
context.Response.Close();
context.Response.End();

我也读过这个https://stackoverflow.com/a/15869303/169714但不知道...

I have also read this https://stackoverflow.com/a/15869303/169714 and have no idea...

当我将 stringbuilder 的输出附加到 <string, string>

I have commented out some lines and get an out of memory when I append the output of a stringbuilder to a dictionary of type <string, string>

有趣的是,当我使用 DotNetZip nuget 时,它起作用了https://dotnetzip.codeplex.com/但我更喜欢原生框架而不是另一个 nuget.

The funny thing is that when I used the DotNetZip nuget, it worked https://dotnetzip.codeplex.com/ But I prefer native framework rather then another nuget.

我已阅读此内容并且可以验证 zip 不会超过 500mb http://bhrnjica.net/2012/07/22/with-net-4-5-10-years-memory-limit-of-2-gb-is-over/

I have read this and can verify that the zip will not exceed 500mb http://bhrnjica.net/2012/07/22/with-net-4-5-10-years-memory-limit-of-2-gb-is-over/

ps 似乎与 8000 次 CreateEntryFromFile 尝试添加 CompressionLevel.NoCompression 以减少内存占用有关,但没有解决我的问题内存不足.

ps seems to be related to having 8000 times CreateEntryFromFile tried to add CompressionLevel.NoCompression to reduce memory footprint, but did not solve my out of memory.

编辑 2:为了测试目的,我已将每个产品的 4 个不同图像减少到一个,但仍然没有内存.尝试从 CreateEntryFromFile 移动到更手动的方法...

edit 2: I have reduced the 4 different images per product to just one for testing purposes and still have out of memory. Tried to move from CreateEntryFromFile to a more manual method...

//zip.CreateEntryFromFile(prodImg, brandName + @"" + dr["ProductPictureName"], CompressionLevel.NoCompression);
ZipArchiveEntry zae = zip.CreateEntry(brandName + @"" + dr["ProductPictureName"]);
using (var fileStream = File.OpenRead(prodImg))
{
    fileStream.CopyTo(zae.Open());
}

这两个选项都不起作用.

Both options do not work.

推荐答案

你需要避免使用 MemoryStream,并尝试尽快写入输出流.也许这样的事情会有所帮助.

You need to avoid the MemoryStream, and try to write to the output stream as soon as possible. Maybe something like this can help.

using (var zip = new ZipArchive(context.Response.OutputStream, ZipArchiveMode.Update, true))
{
    // start loop for 2000 products
    zip.CreateEntryFromFile(prodImg, brandName + @"" + dr["ProductPictureName"]);
    // per product CreateEntryFromFile * 4

    // more lines
    // csvs[tmpName] = value + sb.ToString(); // Also avoid the dictionary and the StringBuilder if you were using it just for debugging.
}

希望有所帮助.

这篇关于创建 ziparchive native c# out of memory memorystream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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