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 ...
随机推荐
- Android While 循环导致的资源占用过高进而导致程序崩溃问题
Timeline: Activity_launch_request time:6562004-14 15:31:25.347: I/dalvikvm(3483): Total arena pages ...
- 使用Varnish+ESI实现静态页面的局部缓存(思路篇)
使用Varnish+ESI实现静态页面的局部缓存(思路篇) 页面静态化是搭建高性能网站必用的招式之一,页面静态化可以有效提升系统响应速度,同时也有利于搜索引擎优化.但在页面静态化后,静态页面之间包含( ...
- excel==>csv==via phpmyadmin (edit php.ini & my.ini)==> MySQL Database
正如同标题, 标题的顺序是 先从Excel表单,保存为csv文档. 步骤: 1.这个可以用linux下的libra office打开 abc.xls 2.用libra office 将 abc.xls ...
- ajax实现分页
使用ajax分页原理:第一步:做一个表格 第二步:封装两个方法.第三步:新建个负责显示页面.第四步:做一个纯PHP处理页面处理要显示页的所有数据 首先使用一张数据比较多的数据库中的表格: 首页面加载后 ...
- Js 使用new关键字调用函数和直接调用函数的区别
最近开始学习js,在看到书上的一个例子时,引发了我的一系列思考: 书上例子: function Person(name,age,job){ var o =new Object(); o.name=na ...
- pch文件的作用和配置
pch文件说白了就是一个头文件,只不过这个头文件的类在全局都可以使用,所以说非常的方便,并不用在每个类里面都写一些重复类的头文件,只要将用到的类的头文件放到pch文件里面就行了,当然了还有就是宏定义, ...
- ssm框架中css被拦截
最近用springmvc spring mybatis框架写程序,请求成功并获得数据,唯独css样式不能加载,但路径正确,css文件编码也是utf-8,用火狐debug总是显示未请求到(都快怀疑自己写 ...
- C++中的结构体vector排序
在包含了头文件#include <algorithm>之后,就可以直接利用sort函数对一个vector进行排序了: // sort algorithm example #include ...
- PHPStrom使用SASS,SCSS和Compass
以前尝试 SASS 的时候写了一篇安装方法,大部分操作还是相同,下面补充一些内容主要是填坑,实在太TMD坑爹了. 参考这篇文章: http://blog.csdn.net/zhouzme/articl ...
- ES6就是ES2015 的主要内容
转自 https://segmentfault.com/a/1190000004365693 ECMAScript 6(以下简称ES6)是JavaScript语言的下一代标准.因为当前版本的ES6是在 ...