以下代码适用于较小的文件(例如100MB),但是对于较大的文件(例如400MB)则抛出System.OutOfMemoryException
。我使用NotNetZip作为dll来获取zip文件。
这是我的代码:
string pathGetDoc = pathDocs + "\\" + informe.NickName + "\\" + getMesActual() + "\\" + informe.Name;
string fileName = informe.Name;
System.Net.WebClient wc = new System.Net.WebClient();
wc.OpenRead(pathGetDoc);
int bytes_total = Convert.ToInt32(wc.ResponseHeaders["Content-Length"].ToString());
if(bytes_total >= 100000000)
{
using (ZipFile zip = new ZipFile())
{
zip.AddFile(pathGetDoc, fileName);
zip.CompressionMethod = CompressionMethod.BZip2;
zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
using (MemoryStream memoryStream = new MemoryStream())
{
zip.Save(memoryStream);
return File(memoryStream.ToArray(), "application/zip", "z.zip");
}
}
}
如您所见,我有一个IF来检查文件的大小,这很好用,但是当该过程保存.zip文件时,出现错误System.OutOfMemoryException