首先通过NuGet管理安装ICSharpCode.SharpZipLib.dll

以下是压缩的通用方法:

using System;
using System.IO;
using System.Web;
using System.Linq;
using System.Collections.Generic;
using ICSharpCode.SharpZipLib.Zip; namespace Common
{
/// <summary>
/// 压缩文件帮助类
/// </summary>
public static class ZipHelper
{
/// <summary>
/// 压缩多个文件
/// </summary>
/// <param name="filesToZip">要压缩的文件的相对路径集合</param>
/// <param name="zipedFileName">压缩后的文件名</param>
     /// <param name="zipPassword">压缩密码</param>
/// <param name="blockSize">每次写入的缓冲区大小</param>
/// <param name="zipLevel">压缩等级(0-9)</param>
/// <returns></returns>
public static string ZipFile(List<string> filesToZip, string zipedFileName, string zipPassword = "", int blockSize = , int zipLevel = )
{
try
{
//压缩后的压缩文件相对路径
var newFileName = @"~/UploadFiles/Temp/" + zipedFileName + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".zip"; //压缩后的压缩文件物理地址
var zipedFilePath = HttpContext.Current.Server.MapPath(newFileName); //获取所有文件的物理地址
List<string> allFilesPath = new List<string>();
if (filesToZip != null && filesToZip.Any())
{
filesToZip.ForEach(file =>
{
var serverPath = HttpContext.Current.Server.MapPath(file);
if (File.Exists(serverPath))
{
allFilesPath.Add(serverPath);
}
});
} if (allFilesPath.Any())
{
//创建临时目录
var directory = HttpContext.Current.Server.MapPath(@"~/UploadFiles/Temp");
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
} //创建压缩文件
ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipedFilePath));
zipStream.SetLevel(zipLevel);
zipStream.Password = zipPassword; //写入所有文件到压缩文件
for (int i = ; i < allFilesPath.Count; i++)
{
string strFilePath = allFilesPath[i];
              FileStream fs = null;
try
{
//被压缩的文件名
string strFileName = strFilePath.Substring(strFilePath.LastIndexOf("\\") + ); ZipEntry entry = new ZipEntry(strFileName);
entry.DateTime = DateTime.Now;
zipStream.PutNextEntry(entry); //读取文件
fs = File.OpenRead(strFilePath); //缓冲区大小
byte[] buffer = new byte[blockSize];
int sizeRead = ;
do
{
sizeRead = fs.Read(buffer, , buffer.Length);
zipStream.Write(buffer, , sizeRead);
}
while (sizeRead > );
}
catch (Exception ex)
{
//continue;
}
                        finally
                        {
                            if (fs != null)
                            {
                                fs.Close();
                                fs.Dispose();
                            }
                        }
} zipStream.Finish();
zipStream.Close();
            //返回压缩后的压缩文件相对路径
return newFileName;
} return string.Empty;
}
catch (Exception ex)
{
return string.Empty;
}
}
}
}

调用:

//要压缩的附件相对路径集合
List<string> filesToZip = new List<string>();
var ziped_file = ZipHelper.ZipFile(filesToZip, "压缩后的文件名");

C#使用ICSharpCode.SharpZipLib.dll压缩多个文件的更多相关文章

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

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

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

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

  3. C#文件或文件夹压缩和解压方法(通过ICSharpCode.SharpZipLib.dll)

    我在网上收集一下文件的压缩和解压的方法,是通过ICSharpCode.SharpZipLib.dll 来实现的 一.介绍的目录 第一步:下载压缩和解压的 ICSharpCode.SharpZipLib ...

  4. C# ICSharpCode.SharpZipLib.dll文件压缩和解压功能类整理,上传文件或下载文件很常用

    工作中我们很多时候需要进行对文件进行压缩,比较通用的压缩的dll就是ICSharpCode.SharpZipLib.dll,废话不多了,网上也有很多的资料,我将其最常用的两个函数整理了一下,提供了一个 ...

  5. C# ZipHelper C#公共类 -- ICSharpCode.SharpZipLib.dll实现压缩和解压

    关于本文档的说明 本文档基于ICSharpCode.SharpZipLib.dll的封装,常用的解压和压缩方法都已经涵盖在内,都是经过项目实战积累下来的 1.基本介绍 由于项目中需要用到各种压缩将文件 ...

  6. C# 压缩文件 ICSharpCode.SharpZipLib.dll

    效果: 代码只能压缩文件夹里面的文件,不能压缩文件夹. 压缩前: 压缩后: 代码: 需要引用ICSharpCode.SharpZipLib.dll public ActionResult Index( ...

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

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

  8. 用ICSharpCode.SharpZipLib进行压缩

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

  9. ICSharpCode.SharpZipLib实现压缩解压缩

    最近,在项目中经常需要处理压缩和解压缩文件的操作.经过查找,发现了ICSharpCode.SharpZipLib.dll ,这是一个完全由c#编写的Zip, GZip.Tar . BZip2 类库,可 ...

随机推荐

  1. ajax 本地测试,使用Chrome 浏览器

    出现问题: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is ...

  2. 在VSTO界面中,调用xll中的函数

    最近研究各种有点迷茫了,原来Xll的加载宏直接可以在C#中调用的,我又各种Out了. 先说明一下,在VBA中,如何调用吧 XLLFound = Application.RegisterXLL(This ...

  3. OC:习题来自平时搜索

    == 第一部分 ==  类变量的@protected ,@private,@public,@package,声明各有什么含义?写一个标准宏MIN,这个宏输入两个参数并返回较小的一个?面向对象的三大特征 ...

  4. 以Outlook样式分组和排列数据项

    转载:http://www.cnblogs.com/peterzb/archive/2009/05/29/1491781.html OutlookGrid:以Outlook样式分组和排列数据项 (这里 ...

  5. Thread message loop for a thread with a hidden window? Make AllocateHwnd safe

    Thread message loop for a thread with a hidden window? I have a Delphi 6 application that has a thre ...

  6. javascript 操作 excel 全攻略

    最近做一个项目,用到了javascript操纵excel以生成报表,下面是标有详细注解的实例 <html> <head><script language="ja ...

  7. 使用visual studio 2013 快速搭建phonegap开发环境

    前一段时间开发了一款简单的Phonegap应用,遇到了很多坑,其中有一个坑就是在搭建开发环境上.由于Phonegap 2.x 与3.x 区别比较大,导致了开发环境也有所不同.2.x 是这样的http: ...

  8. MySQL 5.7.13解压版安装记录 mysql无法启动教程

    1 解压缩 2 添加环境变量,这个不细说了 我的电脑->属性->高级->环境变量 选择PATH,在其后面添加: 你的mysql bin文件夹的路径 (如:C:\Program Fil ...

  9. CSS Sprites图片处理

    简介: CSS Sprites是一个网页图片处理方式,在国内都叫CSS精灵,css Sprites允许你将一个页面涉及到的所有零星图片都包含到一张大图中去,这样一来,当访问该页面时,载入的图片就不会像 ...

  10. 【JavaScript】JavaScript脚本代码的位置及在页面中的执行顺序

    三.如何改变Javascript在页面的执行顺序 利用onload <script type="text/javascript">window.onload = f; ...