Nuget 安装:Install-Package ICSharpCode.SharpZipLib.dll

private void WriteZipFile(string[] filesToZip, string writeToFilePath)
{
try
{
Crc32 crc = new Crc32();
ZipOutputStream s = new ZipOutputStream(System.IO.File.Create(writeToFilePath));
s.SetLevel(9); // 0 - store only to 9 - means best compression
for (int i = 0; i < filesToZip.Length; i++)
{
// Must use a relative path here so that files show up in the Windows Zip File Viewer
// .. hence the use of Path.GetFileName(...)
ZipEntry entry = new ZipEntry(Path.GetFileName(filesToZip[i]));
entry.DateTime = DateTime.Now; // Read in the
using (FileStream fs = System.IO.File.OpenRead(filesToZip[i]))
{ byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length); // set Size and the crc, because the information
// about the size and crc should be stored in the header
// if it is not set it is automatically written in the footer.
// (in this case size == crc == -1 in the header)
// Some ZIP programs have problems with zip files that don't store
// the size and crc in the header.
entry.Size = fs.Length;
fs.Close(); crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
} s.Finish();
s.Close(); }
catch (Exception ex)
{
HttpContext.Trace.Warn(ex.ToString());
}
}

  

今天经过大文件压缩测试,发现这里有一个内存溢出的bug,当文件大于500M左右时,

 byte[] buffer = new byte[fs.Length]; //会发生内存溢出

经查资料,解决方法是,边读文件流边压缩,这样就可以避免Web服务器一次性都到的内存里,如果先把文件压缩到MemoryStream,最后在输出就会消耗大量的内存在MemoryStream里。也就容易引起内存溢出了

private void WriteZipFile(string[] filesToZip, string writeToFilePath)
{
try
{
Stream fs1 = System.IO.File.OpenWrite(writeToFilePath);
ZipOutputStream s = new ZipOutputStream(fs1);
s.SetLevel(9); // 0 - store only to 9 - means best compression
for (int i = 0; i < filesToZip.Length; i++)
{
ZipEntry entry = new ZipEntry(Path.GetFileName(filesToZip[i]));
entry.DateTime = DateTime.Now;
using (var fs = new FileStream(filesToZip[i], FileMode.Open, FileAccess.Read))
{
entry.Size = fs.Length; //这个时候是没用把文件读到内存里的
s.PutNextEntry(entry); byte[] buffer = new byte[10240]; //边读边写
int bytesRead = 0;
while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
{
s.Write(buffer, 0, bytesRead);
}
s.CloseEntry();
}
}
s.Finish();
s.Close();
fs1.Close();
}
catch (Exception ex)
{
HttpContext.Trace.Warn(ex.ToString());
}
}

MVC 输出:File(Server.MapPath("文件路径"), "application/zip", "文件名");

WebForm 输出同样要避免500M文件容易出现的内存溢出:

        public void DownloadFile(string physicalFilePath)
{
FileStream stream = null;
try
{
stream = new FileStream(physicalFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); byte[] buffer = new byte[10240];
int bytesRead = 0;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
{
HttpContext.Response.OutputStream.Write(buffer, 0, bytesRead);
}
HttpContext.Response.ContentType = "application/octet-stream ";
HttpContext.Response.AppendHeader("Content-Disposition ", "attachment;filename= " + System.IO.Path.GetFileName(physicalFilePath)); HttpContext.Response.End();
}
finally
{
stream.Close();
}
}

  

SharpZipLib.dll 压缩文件,可以应用于MVC, webform. C# windows application 等等地方的更多相关文章

  1. C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件

    大家可以到http://www.icsharpcode.net/opensource/sharpziplib/ 下载SharpZiplib的最新版本,本文使用的版本为0.86.0.518,支持Zip, ...

  2. C#使用ICSharpCode.SharpZipLib.dll压缩多个文件

    首先通过NuGet管理安装ICSharpCode.SharpZipLib.dll 以下是压缩的通用方法: using System; using System.IO; using System.Web ...

  3. C# 下利用ICSharpCode.SharpZipLib.dll实现文件/目录压缩、解压缩

    ICSharpCode.SharpZipLib.dll下载地址 1.压缩某个指定文件夹下日志,将日志压缩到CompressionDirectory文件夹中,并清除原来未压缩日志. #region 压缩 ...

  4. C#使用ICSharpCode.SharpZipLib.dll进行文件的压缩与解压

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  5. ICSharpCode.SharpZipLib.dll 压缩多文件

    网站:http://icsharpcode.github.io/SharpZipLib/ 引用:ICSharpCode.SharpZipLib.dll private string CompassZi ...

  6. C#使用SharpZipLib创建压缩文件,并指定压缩文件夹路径(解决SharpZipLib压缩长路径显示问题)

    在项目中使用SharpZipLib压缩文件夹的时候,遇到如果目录较深,则压缩包中的文件夹同样比较深的问题.比如,压缩当前程序目录下的某个文件夹(D:\cx\code\program\bin\debug ...

  7. ICSharpCode.SharpZipLib.Zip 压缩文件

    public class ZipFileHelper { List<string> urls = new List<string>(); void Director(strin ...

  8. AutoFac - 将 autofac 应用于MVC多层项目

    一.前言 AutoFac是.NET平台下的一款著名的IoC Container,它可以让我们很轻松的解除项目中服务类的接口与客户类的接口实现类之间的依赖关系,从而降低系统各模块之间耦合程度以提高系统的 ...

  9. 用ICSharpCode.SharpZipLib进行压缩

    今天过中秋节,当地时间(2013-09-08),公司也放假了,正好也闲着没事,就在网上学习学习,找找资料什么的.最近项目上可能会用到压缩的功能,所以自己就先在网上学习了,发现一个不错的用于压缩的DLL ...

随机推荐

  1. hadoop集群安装

    首现非常感谢 虾皮(http://www.cnblogs.com/xia520pi/archive/2012/05/16/2503949.html) 安装过程是参照他的<Hadoop集群(第5期 ...

  2. Entity Framework:如果允许模型处于非法状态,在某些场景下,记得清空DbContext

    Entity Framework:如果允许模型处于非法状态,在某些场景下,记得清空DbContext 背景 之前写过两篇文章介绍模型的合法性: DDD:关于模型的合法性,Entity.IsValid( ...

  3. Dragon Balls(hdu3635带权并查集)

    题意:n个城市有n个龙珠,T  a,b 代表将龙珠a移动到b城市,Q a代表查询龙珠a所在城市,该城市有多少颗龙珠,该龙珠移动了多少次. 注意:移动时是将龙珠所在该城市所有龙珠都移动,每个龙珠不会再回 ...

  4. Codeforces Round #193 (Div. 2)

    题目地址: http://codeforces.com/contest/332 第一题:题目又臭又长,读了好长时间才读懂. n个人,你是0号,从0开始到n-1循环做动作,只要你前面三个人动作一样,你就 ...

  5. Spring注解用法

    1. Controller层:@Controller @Controller @RequestMapping("/user")//请求localhost:8080/user/*** ...

  6. Python之路 1Day

    1.python基础入门   作者:黄旭 提示: 语法基于python3.5版本(会提示2.7版本和3.5版本的区别) Python命令行将以>>>开始,比如 >>> ...

  7. 昨天CSAPP上的疑问的解答

    昨天CSAPP上的疑问的解答 今天整明白了. CSAPP英文版第2版,826页,或者中文版第2版546页,有这么一段.关于多级页表的. "But if we had a 32-bit add ...

  8. Ecshop出现问题 includes\lib_main.php on line 1329 includes\lib_base.php on line

    php 5.3版本兼容问题不少,以上函数参数传递问题可以将lib_main.php on line 1329这句 $ext = end(explode('.', $tmp)); 改为 : $extsu ...

  9. 关于国产跨平台的开源游戏引擎LGame

    声明:   本博客文章原创类别的均为个人原创,版权所有.转载请注明出处: http://blog.csdn.net/ml3947,另外本人的个人博客:http://www.wjfxgame.com. ...

  10. C++ 头文件系列(deque)

    简介 deque是double ended queue(即双端队列)的简称. 就像C++中的大部分容器的一样,deque具有以下属性: 顺序的(sequence) 动态增长的(dynamic grow ...