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 ...
随机推荐
- WCF客户端与服务端通信简单入门教程
服务端 1.新建空白解决方案,然后再空白解决方案中新建:WCF服务应用程序.建完后如图: 2.删掉自动生成的IService1.cs和Service.svc并添加WCF服务文件StudentServi ...
- Nunit NMock Ncover单元测试
Nunit中如何进行事务性单元测试 单元测试要求:单元测试方法并不真正去变更数据库,也就是说单元测试不依赖于数据库中的数据.那我们如何解决执行单元测试方法后,不变更数据库中数据呢? 一般的解决方案 ...
- hdu 3074 Multiply game(模板级线段树)
离机房关门还有十分钟,这点时间能干些什么?故作沉思地仰望星空,重新捋一下一天的学习进度,或者,砍掉一棵模板级线段树. 纯模板,就是把单点更新,区间求和改为单点更新,区间求积. 1A. #include ...
- mini2440裸机之I2C
// File Name : IIC.c // Function : S3C2440 IIC-bus Master Tx/Rx mode Test Program // (I ...
- TCMalloc小记
周末抽空看了一下tcmalloc,了解了个大概.下面记录一下. 一. 原理 tcmalloc就是一个内存分配器,管理堆内存,主要影响malloc和free,用于降低频繁分配.释放内存造成的性能损耗,并 ...
- Physical Standby Database Failover
1.物理standby failover 切换 故障转移时在一些糟糕的事情发生时执行的计划外事件,需要将生产库移动到DR站点.有意思的是,这时候人们通常忙来忙去,试图弄明白发生了什么,需要做些什么才能 ...
- Memcached 学习笔记(二)——ruby调用
Memcached 学习笔记(二)——ruby调用 上一节我们讲述了怎样安装memcached及memcached常用命令.这一节我们将通过ruby来调用memcached相关操作. 第一步,安装ru ...
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第五章:排序、分页和路由
本章的重点是对产品信息增加排序和分页的功能,以及使用ASP.NET Routing特性添加更加友好的URL支持. 注意:如果你想按照本章的代码编写示例,你必须完成第四章或者直接从www.apress. ...
- oracle——用户
新增用户 用system用户登录 CREATE USER TEST1 IDENTIFIED BY TEST1; CREATE USER:创建用户命令,后跟用户名: IDENTIFIED BY:后跟密码 ...
- LR设置关联---部分内容摘自网络--望见谅
模拟环境---LR机票定票系统设置:首页点击administration-勾选Set LOGIN form's action tag to an error page.选项,点击update. 现在许 ...