使用System.IO.Packaging创建大型ZIP文件时出现OutOfMemoryException [英] OutOfMemoryException when creating large ZIP file using System.IO.Packaging
问题描述
我正在尝试调试使用System.IO.Packaging.ZipPackage
创建相当大的ZIP
文件时出现的OutOfMemoryException
.
I am trying to debug an OutOfMemoryException
that occurs when creating a fairly large ZIP
file using System.IO.Packaging.ZipPackage
.
代码正在遍历大量对象,并对每个对象执行以下操作.
The code is iterating through a large list of objects, doing the following for each object.
- 将对象数据序列化为临时文件.
- 为文件创建
PackagePart
. - 从源
System.IO.Stream
复制到另一个:- 源流:
FileStream
- 目标流:
PackagePart::GetStream()
=>MS.Internal.IO.Zip.ZipIOModeEnforcingStream
- 源流:
- Serializing the object data to a temporary file.
- Creating a
PackagePart
for the file. - Copy from a source
System.IO.Stream
to another:- Source stream:
FileStream
- Target stream:
PackagePart::GetStream()
=>MS.Internal.IO.Zip.ZipIOModeEnforcingStream
- Source stream:
最后,它调用Package::Close()
保存文件.
Finally it calls Package::Close()
which saves the file.
我遇到的问题是,对于一个特别大的对象列表,我看到一个OutOfMemoryException
(x86进程的大小已达到约1.2GB).
The problem I am having is that for a particularly large list of objects, I am seeing an OutOfMemoryException
(the x86 process size is getting to about 1.2GB in size).
我当时正在考虑将对象数据划分为大块,因此每个循环只处理少量数据(即上述步骤1-3).我的想法是,我将在一个临时目录中创建n
ZIP文件,然后找到一种将它们组合到单个存档中的方法.
I was thinking about partitioning the object data into chunks so I only process a smaller amount per loop (i.e. steps 1-3 above). The idea is that I would create n
ZIP files in a temporary directory, and then find a way to combine them into a single archive.
使用System.IO.Packaging
可以吗?我要用什么来组合这些部分?
Is this possible using System.IO.Packaging
? What would I use to combine the parts?
还是有更好的方法来解决此问题?
Or is there a better way to fix this?
推荐答案
在创建新包之间调用Package对象的Flush
方法可能会解决该问题,因为这将导致内存缓冲区刷新到磁盘
Calling the Flush
method on the Package object in between creating a new package should probably solve the problem as that would cause the memory buffer to be flushed to disk.
这篇关于使用System.IO.Packaging创建大型ZIP文件时出现OutOfMemoryException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!