SharpZipLib.dll 压缩文件,可以应用于MVC, webform. C# windows application 等等地方
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 等等地方的更多相关文章
- C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件
大家可以到http://www.icsharpcode.net/opensource/sharpziplib/ 下载SharpZiplib的最新版本,本文使用的版本为0.86.0.518,支持Zip, ...
- C#使用ICSharpCode.SharpZipLib.dll压缩多个文件
首先通过NuGet管理安装ICSharpCode.SharpZipLib.dll 以下是压缩的通用方法: using System; using System.IO; using System.Web ...
- C# 下利用ICSharpCode.SharpZipLib.dll实现文件/目录压缩、解压缩
ICSharpCode.SharpZipLib.dll下载地址 1.压缩某个指定文件夹下日志,将日志压缩到CompressionDirectory文件夹中,并清除原来未压缩日志. #region 压缩 ...
- C#使用ICSharpCode.SharpZipLib.dll进行文件的压缩与解压
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- ICSharpCode.SharpZipLib.dll 压缩多文件
网站:http://icsharpcode.github.io/SharpZipLib/ 引用:ICSharpCode.SharpZipLib.dll private string CompassZi ...
- C#使用SharpZipLib创建压缩文件,并指定压缩文件夹路径(解决SharpZipLib压缩长路径显示问题)
在项目中使用SharpZipLib压缩文件夹的时候,遇到如果目录较深,则压缩包中的文件夹同样比较深的问题.比如,压缩当前程序目录下的某个文件夹(D:\cx\code\program\bin\debug ...
- ICSharpCode.SharpZipLib.Zip 压缩文件
public class ZipFileHelper { List<string> urls = new List<string>(); void Director(strin ...
- AutoFac - 将 autofac 应用于MVC多层项目
一.前言 AutoFac是.NET平台下的一款著名的IoC Container,它可以让我们很轻松的解除项目中服务类的接口与客户类的接口实现类之间的依赖关系,从而降低系统各模块之间耦合程度以提高系统的 ...
- 用ICSharpCode.SharpZipLib进行压缩
今天过中秋节,当地时间(2013-09-08),公司也放假了,正好也闲着没事,就在网上学习学习,找找资料什么的.最近项目上可能会用到压缩的功能,所以自己就先在网上学习了,发现一个不错的用于压缩的DLL ...
随机推荐
- MySQL 常用命令大全2
下面贴出我在实际工作中遇到mysql操作数据表的sql命令,如有不对的地方,请多指教: c++链接mysql头文件命令 g++ is_in_polygon.cpp -o is_in_polygon - ...
- 构建RESTful风格的WCF服务
构建RESTful风格的WCF服务 RESTful Wcf是一种基于Http协议的服务架构风格. 相较 WCF.WebService 使用 SOAP.WSDL.WS-* 而言,几乎所有的语言和网络平台 ...
- spring添加通知配置
在项目里添加的spring配置文件 <bean id="beforeMethod" class="com.wxw.core.common.AdviceBefore& ...
- [置顶] “河软CSDN2011级表彰暨实习动员大会”顺利召开!
9点30分 伴随着激昂的开场曲,主持人走到台前!“河软CSDN2011级表彰暨 实习动员大会即将开始,请各位嘉宾入场!”他们分别是“CSDN教育事业部总经 理李天山先生”“河北软件职业技术学院 软件工 ...
- Qt Creator+MinGW+boost特殊函数的使用示例
Qt Creator+MinGW+boost特殊函数的使用示例: 先编译和安装boost: bootstrap.bat gcc .\b2 --toolset=gcc --prefix=E:\boost ...
- Android 关于ListView中按钮监听的优化问题(方法二)
关于ListView中按钮监听的优化问题(方法一)地址: http://www.cnblogs.com/steffen/p/3951901.html 之前的方法一,虽然能够解决position的传递, ...
- NUTZ中处理系统未捕获异常
关键内容 mvc-chain.js ViewProcessor ai.setFailView(“redirect:/sysError.html”); log.error(this.trrowableT ...
- 控制 Memory 和 CPU 资源的使用
Resource Governor的出现,解决了在一台SQL Server实例上,管理多用户工作负载和资源隔离的需求,它允许管理员限制系统处理Requsts时所耗费的CPU 和 Memory资源的数量 ...
- iOS开发-内存管理
内存管理 对于这篇呢,其实现在都是ARC模式,正常状态下基本不用我们去手动释放内存,所以如果不是要面试呀.装逼或者扎实功底的,就先别看了或者了解下即可,因为像面试时,有些面试官想看你的基础时,就有些人 ...
- 2017 年不可错过的开发工具 Top 50
想知道 2017 年有哪些值得关注的开发工具吗?StackShare 年度开发工具排行榜来啦! StackShare.io 是一个开发者工具及服务分享平台,致力于发现并分享开发者使用的开发工具.服务与 ...