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 ...
随机推荐
- 关于前端JS模块加载器实现的一些细节
最近工作需要,实现一个特定环境的模块加载方案,实现过程中有一些技术细节不解,便参考 了一些项目的api设计约定与实现,记录下来备忘. 本文不探讨为什么实现模块化,以及模块化相关的规范,直接考虑一些技术 ...
- LigerUI权限系统之菜单管理
菜单管理跟上篇的组织结构实现方式差不多,所以不存在多大的问题.还是老样,先上图,再看代码.菜单管理界面 菜单管理添加: 菜单管理修改: 前端完整代码: @section headerScripts{ ...
- C# 线程池的使用
C#使用Thread多线程时,经常会用到多个线程都执行完后再进行下一步操作,以前的做法是设置全局变量来标识,后来看到了线程池的概念,简单的记录下来,示例代码如下: ManualResetEvent m ...
- HTTPCLIENT抓取网页内容
通过httpclient抓取网页信息. public class SnippetHtml{ /** * 通过url获取网站html * @param url 网站url */ public Strin ...
- oracle表空间表分区详解及oracle表分区查询使用方法(转+整理)
欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...
- node.JS中配置http-server
http-server 是一个简单的HTTP服务器, 基于 nodeJs,在nodejs命令行中配置http服务器. 项目结构:
- shell 分支/循环
==)); then patern="Update" else patern="Read" fi in "-h") ] then helpI ...
- 前端——HTML笔记Two
web前端需要掌握:HTML.CSS.JavaScript语言.这三门技术用来实现: 1. HTML是网页内容的载体.内容就是网页制作者放在页面上想要让用户浏览的信息,可以包含文字.图片.视频等. 2 ...
- 如何用php开启企业微信开发的回调模式
猜想: 懵逼 实践: 微信公众号开发的手册中甚至给出了只需要修改几个参数就能使用的范例.企业微信开发中在一个很不显眼的地方放了一个sample. https://work.weixin.qq.com/ ...
- Linux 复习重点目录
Linux安全复习 一.Linux基本命令 1.文件管理命令 lvm 2.用户管理命令 3.网络管理命令 4.权限管理 普通权限和特殊权限 权限命令修改 5.服务命令 6.软件安装管理命令 yum安装 ...