Cat*_*lMF 3 c# compression out-of-memory sharpziplib
我有一个 453MB XML 文件,我正在尝试使用SharpZipLib将其压缩为 ZIP 。
下面是我用来创建 zip 的代码,但它导致了OutOfMemoryException. 此代码成功压缩了 428MB 的文件。
知道为什么会发生异常,因为我不明白为什么,因为我的系统有足够的可用内存。
public void CompressFiles(List<string> pathnames, string zipPathname)
{
try
{
using (FileStream stream = new FileStream(zipPathname, FileMode.Create, FileAccess.Write, FileShare.None))
{
using (ZipOutputStream stream2 = new ZipOutputStream(stream))
{
foreach (string str in pathnames)
{
FileStream stream3 = new FileStream(str, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] buffer = new byte[stream3.Length];
try
{
if (stream3.Read(buffer, 0, buffer.Length) != buffer.Length)
{
throw new Exception(string.Format("Error reading '{0}'.", str));
}
}
finally
{
stream3.Close();
}
ZipEntry entry = new ZipEntry(Path.GetFileName(str));
stream2.PutNextEntry(entry);
stream2.Write(buffer, 0, buffer.Length);
}
stream2.Finish();
}
}
}
catch (Exception)
{
File.Delete(zipPathname);
throw;
}
}
Run Code Online (Sandbox Code Playgroud)
您正在尝试创建一个与文件一样大的缓冲区。相反,使缓冲区大小固定,读取一些字节到其中,并将读取的字节数写入 zip 文件中。
这是带有 4096 字节缓冲区的代码(以及一些清理):
public static void CompressFiles(List<string> pathnames, string zipPathname)
{
const int BufferSize = 4096;
byte[] buffer = new byte[BufferSize];
try
{
using (FileStream stream = new FileStream(zipPathname,
FileMode.Create, FileAccess.Write, FileShare.None))
using (ZipOutputStream stream2 = new ZipOutputStream(stream))
{
foreach (string str in pathnames)
{
using (FileStream stream3 = new FileStream(str,
FileMode.Open, FileAccess.Read, FileShare.Read))
{
ZipEntry entry = new ZipEntry(Path.GetFileName(str));
stream2.PutNextEntry(entry);
int read;
while ((read = stream3.Read(buffer, 0, buffer.Length)) > 0)
{
stream2.Write(buffer, 0, read);
}
}
}
stream2.Finish();
}
}
catch (Exception)
{
File.Delete(zipPathname);
throw;
}
}
Run Code Online (Sandbox Code Playgroud)
特别注意这个块:
const int BufferSize = 4096;
byte[] buffer = new byte[BufferSize];
// ...
int read;
while ((read = stream3.Read(buffer, 0, buffer.Length)) > 0)
{
stream2.Write(buffer, 0, read);
}
Run Code Online (Sandbox Code Playgroud)
这会将字节读入buffer. 当没有更多字节时,该Read()方法返回 0,此时我们就停止了。当Read()成功时,我们可以确定缓冲区中有一些数据,但我们不知道有多少字节。整个缓冲区可能被填满,或者只填满其中的一小部分。因此,我们使用读取的字节数read来确定写入多少字节ZipOutputStream。
顺便说一下,该代码块可以用添加到 .Net 4.0 中的简单语句替换,其作用完全相同:
stream3.CopyTo(stream2);
Run Code Online (Sandbox Code Playgroud)
所以,你的代码可能会变成:
public static void CompressFiles(List<string> pathnames, string zipPathname)
{
try
{
using (FileStream stream = new FileStream(zipPathname,
FileMode.Create, FileAccess.Write, FileShare.None))
using (ZipOutputStream stream2 = new ZipOutputStream(stream))
{
foreach (string str in pathnames)
{
using (FileStream stream3 = new FileStream(str,
FileMode.Open, FileAccess.Read, FileShare.Read))
{
ZipEntry entry = new ZipEntry(Path.GetFileName(str));
stream2.PutNextEntry(entry);
stream3.CopyTo(stream2);
}
}
stream2.Finish();
}
}
catch (Exception)
{
File.Delete(zipPathname);
throw;
}
}
Run Code Online (Sandbox Code Playgroud)
现在您知道为什么会出现错误,以及如何使用缓冲区。