使用 BZip2 (SharpZipLib) 时出现 OutOfMemoryException
我使用Asp.net、.net 3.5、win2003、iis 6.0。
我使用 Oracle 收集文件,将文件以 SharpZipLib.BZip2 压缩格式保存在 Oracle 表的 RAW 字段中。
我的应用程序是 Web,我使用 WCF 服务来获取文件的数据(字节数组)。 aspx 页面将文件发送给用户(下载文件)。
我的问题:
我从 Oracle 读取数据(我调用 WCF 服务)。我得到字节数组(byte[]),
我尝试使用 SharpZipLib.BZip2 解压缩文件
using (MemoryStream inData = new MemoryStream(data))
{
using (MemoryStream outData = new MemoryStream())
{
BZip2.Decompress(inData, outData); //<==================== Fails here OutOfMemoryException
return outData.ToArray();
}
}
,错误是因为“未压缩”文件很大,非常大(> 500 MB)!
压缩文件:4MB
未压缩文件:> 500 MB
我做这样的测试:
BufferedStream bufin = new BufferedStream(instream);
using (MemoryStream outData = new MemoryStream())
{
BZip2.Decompress(bufin, outData);
return outData.ToArray();
}
但我得到相同的 OutOfMemoryException
跟踪堆栈。解压缩
en System.IO.MemoryStream.set_Capacity(Int32 value)
en System.IO.MemoryStream.EnsureCapacity(Int32 value)
en System.IO.MemoryStream.WriteByte(Byte value)
en Reale.Frk.Compression.BZip2.BZip2.Decompress(Stream inStream, Stream outStream)
异常代码的 SharpZipLib.BZip2
public static void Decompress(Stream inStream, Stream outStream)
{
if ( inStream == null ) {
throw new ArgumentNullException("inStream");
}
if ( outStream == null ) {
throw new ArgumentNullException("outStream");
}
using ( outStream ) {
using ( BZip2InputStream bzis = new BZip2InputStream(inStream) ) {
int ch = bzis.ReadByte();
while (ch != -1) {
outStream.WriteByte((byte)ch);
ch = bzis.ReadByte();
}
}
}
}
任何建议,评论,示例源代码?
I use Asp.net , .net 3.5, win2003, iis 6.0.
I use Oracle for gathering files, saving file in SharpZipLib.BZip2 compressed format in field RAW in table Oracle.
My application is Web, and I use WCF Service for get data (array of bytes) of a file. The aspx page send file to user (download file).
My issue-problem:
I read DATA from Oracle, (I call to WCF Service). I get array of bytes (byte[]),
I try Uncompress file using SharpZipLib.BZip2
using (MemoryStream inData = new MemoryStream(data))
{
using (MemoryStream outData = new MemoryStream())
{
BZip2.Decompress(inData, outData); //<==================== Fails here OutOfMemoryException
return outData.ToArray();
}
}
the error is because the file "uncompressed" is big, very big (> 500 MB) !!!
compressed file: 4MB
uncompressed file: > 500 MB
I do test like this:
BufferedStream bufin = new BufferedStream(instream);
using (MemoryStream outData = new MemoryStream())
{
BZip2.Decompress(bufin, outData);
return outData.ToArray();
}
But I get the same OutOfMemoryException
Trace Stack of Exception
en System.IO.MemoryStream.set_Capacity(Int32 value)
en System.IO.MemoryStream.EnsureCapacity(Int32 value)
en System.IO.MemoryStream.WriteByte(Byte value)
en Reale.Frk.Compression.BZip2.BZip2.Decompress(Stream inStream, Stream outStream)
Code of SharpZipLib.BZip2.Decompress
public static void Decompress(Stream inStream, Stream outStream)
{
if ( inStream == null ) {
throw new ArgumentNullException("inStream");
}
if ( outStream == null ) {
throw new ArgumentNullException("outStream");
}
using ( outStream ) {
using ( BZip2InputStream bzis = new BZip2InputStream(inStream) ) {
int ch = bzis.ReadByte();
while (ch != -1) {
outStream.WriteByte((byte)ch);
ch = bzis.ReadByte();
}
}
}
}
any suggestions, comments, sample source code ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
跳过
MemoryStream
并直接写入文件。否则向服务器添加更多内存。
指定 MemoryStream 初始容量的另一个选项。
Skip the
MemoryStream
and write directly to a file.Else add more memory to the server.
Another option to specify the initial capacity for the
MemoryStream
.您可能会遇到内存不足错误,因为您的系统上没有 500 MB 内存流的单个连续内存区域,但您可能有足够的非连续内存块。使用 MemoryTributary 类代替,它可能会起作用。该类可能需要一些调整(如果我没记错的话,它可能不会干净地返回最后一个块并用 ASCII(0) 填充它)
Chances are that you are getting out of memory error because the single contiguous memory area for the memory stream of 500 MB is not available on your system, but you may have enough non-contiguous memory blocks. Use MemoryTributary class instead and it might work. The class may require some tweaking (if I remember correctly it may not cleanly return the very last block and pad it with ASCII(0))