C#使用ICSharpCode.SharpZipLib.dll压缩多个文件
首先通过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压缩多个文件的更多相关文章
- ICSharpCode.SharpZipLib.dll 压缩多文件
网站:http://icsharpcode.github.io/SharpZipLib/ 引用:ICSharpCode.SharpZipLib.dll private string CompassZi ...
- C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件
大家可以到http://www.icsharpcode.net/opensource/sharpziplib/ 下载SharpZiplib的最新版本,本文使用的版本为0.86.0.518,支持Zip, ...
- C#文件或文件夹压缩和解压方法(通过ICSharpCode.SharpZipLib.dll)
我在网上收集一下文件的压缩和解压的方法,是通过ICSharpCode.SharpZipLib.dll 来实现的 一.介绍的目录 第一步:下载压缩和解压的 ICSharpCode.SharpZipLib ...
- C# ICSharpCode.SharpZipLib.dll文件压缩和解压功能类整理,上传文件或下载文件很常用
工作中我们很多时候需要进行对文件进行压缩,比较通用的压缩的dll就是ICSharpCode.SharpZipLib.dll,废话不多了,网上也有很多的资料,我将其最常用的两个函数整理了一下,提供了一个 ...
- C# ZipHelper C#公共类 -- ICSharpCode.SharpZipLib.dll实现压缩和解压
关于本文档的说明 本文档基于ICSharpCode.SharpZipLib.dll的封装,常用的解压和压缩方法都已经涵盖在内,都是经过项目实战积累下来的 1.基本介绍 由于项目中需要用到各种压缩将文件 ...
- C# 压缩文件 ICSharpCode.SharpZipLib.dll
效果: 代码只能压缩文件夹里面的文件,不能压缩文件夹. 压缩前: 压缩后: 代码: 需要引用ICSharpCode.SharpZipLib.dll public ActionResult Index( ...
- C# 下利用ICSharpCode.SharpZipLib.dll实现文件/目录压缩、解压缩
ICSharpCode.SharpZipLib.dll下载地址 1.压缩某个指定文件夹下日志,将日志压缩到CompressionDirectory文件夹中,并清除原来未压缩日志. #region 压缩 ...
- 用ICSharpCode.SharpZipLib进行压缩
今天过中秋节,当地时间(2013-09-08),公司也放假了,正好也闲着没事,就在网上学习学习,找找资料什么的.最近项目上可能会用到压缩的功能,所以自己就先在网上学习了,发现一个不错的用于压缩的DLL ...
- ICSharpCode.SharpZipLib实现压缩解压缩
最近,在项目中经常需要处理压缩和解压缩文件的操作.经过查找,发现了ICSharpCode.SharpZipLib.dll ,这是一个完全由c#编写的Zip, GZip.Tar . BZip2 类库,可 ...
随机推荐
- c++常见输入方法[持续更新]
字符串输入 cin>> 使用空格确认字符串结束位置,保留换行符在输入队列当中 单个字符读取 cin.get(), cin.get(char) 每次读取一行: cin.getline(cha ...
- iPhone中国移动收不到彩信,联通不用设置都可以,具体设置方法:
打开“设置”. 打开“通用”. 打开“蜂窝移动网络”. 打开“蜂窝数据移动网络”. 在“蜂窝移动数据”一栏中的“APN”处填入“cmnet”. 在“彩信”一栏中的“APN”处填入“cmnet”,“MM ...
- Merge into 使用
在进行SQL语句编写时,我们经常会遇到这样的问题:当存在记录时,就更新(Update),不存在数据时,就插入(Insert),oracle为我们提供了一种解决方法——Merge into ,具体语法如 ...
- Flex 拾色器改变背景
package { import flash.display.*; import flash.geom.*; import flash.utils.*; import mx.core.EdgeMe ...
- oracle 查詢表字段明細、字段注釋、表註釋
查詢表字段明細 select column_name,data_type,data_length,DATA_PRECISION ,DATA_SCALE from all_tab_columns wh ...
- NotePad++ 列模式(在多行开头统一添加相同内容)
==> 按住Alt键不放,用鼠标左键从第一行的开头处按住向下拉,直到所有行 松开Alt键和鼠标左键,你会发现光标变成了一条跨越所有行的竖线 ==> 如果不想使用鼠标操作,可以使用 Alt+ ...
- bootstrap适配移动端
上次在pythonanywhere上挂上去的页面,是这个样子的 而在手机上看是这个样子的 总之简直不能看= = 看了一下学校几个微信公众号的页面.都是用的bootstrap,好吧我也去试试看好了. 在 ...
- Java组各任务工作流程
1.周枫 A.提供基于SQL SERVER的数据库基本表结构创建脚本,基础数据脚本,按学科(产品)的数据脚本. 2.吴缤 A.提供给周茉的安装包用的项目文件,共三个digital,xylinkWeb和 ...
- HDU 4287 Intelligent IME hash
Intelligent IME Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- .cmd文件不小心管理记事本打开的恢复
比如不小心将.cmd文件关联成用记事本打开了,此时须要删除注冊表: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explor ...