C#使用ICSharpCode.SharpZipLib.dll压缩多个文件
https://www.cnblogs.com/zhuyongblogs/p/5178164.html
首先通过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 = 2048, int zipLevel = 9)
{
try
{
//压缩后的压缩文件相对路径
var newFileName = @"~/UploadFiles/Temp/" + zipedFileName + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".zip";</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">压缩后的压缩文件物理地址</span> <span style="color: rgba(0, 0, 255, 1)">var</span> zipedFilePath =<span style="color: rgba(0, 0, 0, 1)"> HttpContext.Current.Server.MapPath(newFileName); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取所有文件的物理地址</span> List<<span style="color: rgba(0, 0, 255, 1)">string</span>> allFilesPath = <span style="color: rgba(0, 0, 255, 1)">new</span> List<<span style="color: rgba(0, 0, 255, 1)">string</span>><span style="color: rgba(0, 0, 0, 1)">(); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (filesToZip != <span style="color: rgba(0, 0, 255, 1)">null</span> &&<span style="color: rgba(0, 0, 0, 1)"> filesToZip.Any()) { filesToZip.ForEach(file </span>=><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">var</span> serverPath =<span style="color: rgba(0, 0, 0, 1)"> HttpContext.Current.Server.MapPath(file); </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (File.Exists(serverPath)) { allFilesPath.Add(serverPath); } }); } </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (allFilesPath.Any()) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建临时目录</span> <span style="color: rgba(0, 0, 255, 1)">var</span> directory = HttpContext.Current.Server.MapPath(<span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">~/UploadFiles/Temp</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">Directory.Exists(directory)) { Directory.CreateDirectory(directory); } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建压缩文件</span> ZipOutputStream zipStream = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ZipOutputStream(File.Create(zipedFilePath)); zipStream.SetLevel(zipLevel); zipStream.Password </span>=<span style="color: rgba(0, 0, 0, 1)"> zipPassword; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">写入所有文件到压缩文件</span> <span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = <span style="color: rgba(128, 0, 128, 1)">0</span>; i < allFilesPath.Count; i++<span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 0, 255, 1)">string</span> strFilePath =<span style="color: rgba(0, 0, 0, 1)"> allFilesPath[i];<br> FileStream fs = null; </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">被压缩的文件名</span> <span style="color: rgba(0, 0, 255, 1)">string</span> strFileName = strFilePath.Substring(strFilePath.LastIndexOf(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">\\</span><span style="color: rgba(128, 0, 0, 1)">"</span>) + <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">); ZipEntry entry </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ZipEntry(strFileName); entry.DateTime </span>=<span style="color: rgba(0, 0, 0, 1)"> DateTime.Now; zipStream.PutNextEntry(entry); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">读取文件</span> fs =<span style="color: rgba(0, 0, 0, 1)"> File.OpenRead(strFilePath); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">缓冲区大小</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[] buffer = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">byte</span><span style="color: rgba(0, 0, 0, 1)">[blockSize]; </span><span style="color: rgba(0, 0, 255, 1)">int</span> sizeRead = <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">do</span><span style="color: rgba(0, 0, 0, 1)"> { sizeRead </span>= fs.Read(buffer, <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">, buffer.Length); zipStream.Write(buffer, </span><span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">, sizeRead); } </span><span style="color: rgba(0, 0, 255, 1)">while</span> (sizeRead > <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">catch <span style="color: rgba(0, 0, 255, 1)"><span style="color: rgba(0, 0, 0, 1)">(Exception ex)</span></span></span><span style="color: rgba(0, 0, 0, 1)"> { //</span><span style="color: rgba(0, 0, 255, 1)">continue</span><span style="color: rgba(0, 0, 0, 1)">; }<br> <span style="color: rgba(0, 0, 255, 1)">finally</span><br> {<br> if (fs != null)<br> {<br> fs.Close();<br> fs.Dispose();<br> }<br> } } zipStream.Finish(); zipStream.Close();<br>
//返回压缩后的压缩文件相对路径
return newFileName;
}</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">.Empty; } </span><span style="color: rgba(0, 0, 255, 1)">catch<span style="color: rgba(0, 0, 0, 1)"> (Exception ex)</span></span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">.Empty; } } }
}
调用:
//要压缩的附件相对路径集合
List<string> filesToZip = new List<string>();
var ziped_file = ZipHelper.ZipFile(filesToZip, "压缩后的文件名");