首先通过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. jQuery plugins 图片上传

    http://plugins.jquery.com/ 用到一下插件: magnific popup 看大图 jQuery File Upload 多文件上传 jQuery Rotate 图片旋转 gi ...

  2. Objective-C 学习记录--toches、Motion/Size/Rect/Point/CGFloat/protocol

    - (void)touchesBegan touchesEnd touchesCancelled touchesMoved //代表的是手指在屏幕上的动作,开始 结束 取消 移动 //还有就是代表摇动 ...

  3. OC:Block语法、Block使用、Block实现数组排序

    Block //定义一个求两个数最大值函数 int maxValue (int ,int); //函数的实现 int maxValue (int a, int b){ return  a > b ...

  4. Java学习笔记之==与equals

    一.问题引入 Java测试两个变量是否相等有两种方式:==运算符和equals方法. 但是这二者完全一样吗?考虑下面程序: public class TestEqual { public static ...

  5. Objective-C语法之代码块(block)的使用

    代码块本质上是和其它变量相似.不同的是,代码块存储的数据是一个函数体.使用代码块是,你能够像调用其它标准函数一样,传入參数数,并得到返回值. 脱字符(^)是块的语法标记.依照我们熟悉的參数语法规约所定 ...

  6. 让python整型计算结果为浮点型

    这是个入门知识,我记录下. 在python中,默认情况下,如果表达式中全是整型,结果也会仅仅是整型.有时候没注意会出现意想不到的结果,比如: >>> a = 7 >>&g ...

  7. hdu 1257 小希的迷宫 并查集

    小希的迷宫 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1272 D ...

  8. Codeforces Round #308 (Div. 2)B. Vanya and Books 数学

    B. Vanya and Books Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/552/pr ...

  9. [Angular2 Router] Lazy Load Angular 2 Modules with the Router

    Angular 2 lazy loading is a core feature of Angular 2. Lazy loading allows your application to start ...

  10. MPI编程简单介绍

    第三章MPI编程 3.1 MPI简单介绍 多线程是一种便捷的模型,当中每一个线程都能够訪问其他线程的存储空间.因此,这样的模型仅仅能在共享存储系统之间移植.一般来讲,并行机不一定在各处理器之间共享存储 ...