首先通过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. Android PHP 通过JSON进行数据交互

    一.首先是Android客户端解析PHP返回的JSON数据 1.PHP代码(这里用到了数据库,如果没有准备数据库的话,可以自定义字符串) <?php $link=mysql_connect(SA ...

  2. Flex坐标

    flash和flex针对不同的目的,提供了3种不同的坐标系. 全局的就是(stage级别的) 本地坐标系(组件级别的) 内容坐标系(相对于本地坐标系说的) 这些坐标系的点是可以转换的,并且有相应的方法 ...

  3. uva539 The Settlers of Catan

    The Settlers of Catan Within Settlers of Catan, the 1995 German game of the year, players attempt to ...

  4. FluentData官方文档翻译

    开始 要求 .NET 4.0. 支持的数据库 MS SQL Server using the native .NET driver. MS SQL Azure using the native .NE ...

  5. 导出excel——入门

    简单介绍 Jxl使用总结 样例 java使用jxl操作Excel文件总结

  6. Codeforces Round #310 (Div. 1) A. Case of Matryoshkas 水题

    C. String Manipulation 1.0 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  7. TOJ3651确定比赛名次

    确定比赛名次   Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByte Total Submit: 23          ...

  8. [Angular2 Router] Optional Route Query Parameters - The queryParams Directive and the Query Parameters Observable

    In this tutorial we are going to learn how to use the Angular 2 router to pass optional query parame ...

  9. java Date比较

    package com.horizon.test; import org.apache.commons.lang.time.DateUtils; public class Hello3 { publi ...

  10. java 验证码图片处理类,为验证码识别做准备

    /* * To change this template, choose Tools | Templates * and open the template in the editor. */pack ...