C#使用ICSharpCode.SharpZipLib.dll压缩多个文件



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

C#使用ICSharpCode.SharpZipLib.dll压缩多个文件_文件名

以下是压缩的通用方法:



C#使用ICSharpCode.SharpZipLib.dll压缩多个文件_相对路径_02

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 >//</span><span >压缩后的压缩文件物理地址</span>
<span >var</span> zipedFilePath =<span > HttpContext.Current.Server.MapPath(newFileName);

</span><span >//</span><span >获取所有文件的物理地址</span>
List<<span >string</span>> allFilesPath = <span >new</span> List<<span >string</span>><span >();
</span><span >if</span> (filesToZip != <span >null</span> &&<span > filesToZip.Any())
{
filesToZip.ForEach(file </span>=><span >
{
</span><span >var</span> serverPath =<span > HttpContext.Current.Server.MapPath(file);
</span><span >if</span><span > (File.Exists(serverPath))
{
allFilesPath.Add(serverPath);
}
});
}

</span><span >if</span><span > (allFilesPath.Any())
{
</span><span >//</span><span >创建临时目录</span>
<span >var</span> directory = HttpContext.Current.Server.MapPath(<span >@"</span><span >~/UploadFiles/Temp</span><span >"</span><span >);
</span><span >if</span> (!<span >Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}

</span><span >//</span><span >创建压缩文件</span>
ZipOutputStream zipStream = <span >new</span><span > ZipOutputStream(File.Create(zipedFilePath));
zipStream.SetLevel(zipLevel);
zipStream.Password </span>=<span > zipPassword;

</span><span >//</span><span >写入所有文件到压缩文件</span>
<span >for</span> (<span >int</span> i = <span >0</span>; i < allFilesPath.Count; i++<span >)
{
</span><span >string</span> strFilePath =<span > allFilesPath[i];<br>              FileStream fs = null;
</span><span >try</span><span >
{
</span><span >//</span><span >被压缩的文件名</span>
<span >string</span> strFileName = strFilePath.Substring(strFilePath.LastIndexOf(<span >"</span><span >\\</span><span >"</span>) + <span >1</span><span >);

ZipEntry entry </span>= <span >new</span><span > ZipEntry(strFileName);
entry.DateTime </span>=<span > DateTime.Now;
zipStream.PutNextEntry(entry);

</span><span >//</span><span >读取文件</span>
fs =<span > File.OpenRead(strFilePath);

</span><span >//</span><span >缓冲区大小</span>
<span >byte</span>[] buffer = <span >new</span> <span >byte</span><span >[blockSize];
</span><span >int</span> sizeRead = <span >0</span><span >;
</span><span >do</span><span >
{
sizeRead </span>= fs.Read(buffer, <span >0</span><span >, buffer.Length);
zipStream.Write(buffer, </span><span >0</span><span >, sizeRead);
}
</span><span >while</span> (sizeRead > <span >0</span><span >);
}
</span><span >catch <span ><span >(Exception ex)</span></span></span><span >
{
//</span><span >continue</span><span >;
}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span >finally</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (fs != null)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fs.Close();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fs.Dispose();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
}

zipStream.Finish();
zipStream.Close();<br>

            //返回压缩后的压缩文件相对路径
return newFileName;

}

</span><span >return</span> <span >string</span><span >.Empty;
}
</span><span >catch<span > (Exception ex)</span></span><span >
{
</span><span >return</span> <span >string</span><span >.Empty;
}
}
}

}


C#使用ICSharpCode.SharpZipLib.dll压缩多个文件_压缩文件_03


调用:



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


 


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

C#使用ICSharpCode.SharpZipLib.dll压缩多个文件_文件名

以下是压缩的通用方法:



C#使用ICSharpCode.SharpZipLib.dll压缩多个文件_c#_05

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 >//</span><span >压缩后的压缩文件物理地址</span>
<span >var</span> zipedFilePath =<span > HttpContext.Current.Server.MapPath(newFileName);

</span><span >//</span><span >获取所有文件的物理地址</span>
List<<span >string</span>> allFilesPath = <span >new</span> List<<span >string</span>><span >();
</span><span >if</span> (filesToZip != <span >null</span> &&<span > filesToZip.Any())
{
filesToZip.ForEach(file </span>=><span >
{
</span><span >var</span> serverPath =<span > HttpContext.Current.Server.MapPath(file);
</span><span >if</span><span > (File.Exists(serverPath))
{
allFilesPath.Add(serverPath);
}
});
}

</span><span >if</span><span > (allFilesPath.Any())
{
</span><span >//</span><span >创建临时目录</span>
<span >var</span> directory = HttpContext.Current.Server.MapPath(<span >@"</span><span >~/UploadFiles/Temp</span><span >"</span><span >);
</span><span >if</span> (!<span >Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}

</span><span >//</span><span >创建压缩文件</span>
ZipOutputStream zipStream = <span >new</span><span > ZipOutputStream(File.Create(zipedFilePath));
zipStream.SetLevel(zipLevel);
zipStream.Password </span>=<span > zipPassword;

</span><span >//</span><span >写入所有文件到压缩文件</span>
<span >for</span> (<span >int</span> i = <span >0</span>; i < allFilesPath.Count; i++<span >)
{
</span><span >string</span> strFilePath =<span > allFilesPath[i];<br>              FileStream fs = null;
</span><span >try</span><span >
{
</span><span >//</span><span >被压缩的文件名</span>
<span >string</span> strFileName = strFilePath.Substring(strFilePath.LastIndexOf(<span >"</span><span >\\</span><span >"</span>) + <span >1</span><span >);

ZipEntry entry </span>= <span >new</span><span > ZipEntry(strFileName);
entry.DateTime </span>=<span > DateTime.Now;
zipStream.PutNextEntry(entry);

</span><span >//</span><span >读取文件</span>
fs =<span > File.OpenRead(strFilePath);

</span><span >//</span><span >缓冲区大小</span>
<span >byte</span>[] buffer = <span >new</span> <span >byte</span><span >[blockSize];
</span><span >int</span> sizeRead = <span >0</span><span >;
</span><span >do</span><span >
{
sizeRead </span>= fs.Read(buffer, <span >0</span><span >, buffer.Length);
zipStream.Write(buffer, </span><span >0</span><span >, sizeRead);
}
</span><span >while</span> (sizeRead > <span >0</span><span >);
}
</span><span >catch <span ><span >(Exception ex)</span></span></span><span >
{
//</span><span >continue</span><span >;
}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span >finally</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs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bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
}

zipStream.Finish();
zipStream.Close();<br>

            //返回压缩后的压缩文件相对路径
return newFileName;

}

</span><span >return</span> <span >string</span><span >.Empty;
}
</span><span >catch<span > (Exception ex)</span></span><span >
{
</span><span >return</span> <span >string</span><span >.Empty;
}
}
}

}


C#使用ICSharpCode.SharpZipLib.dll压缩多个文件_压缩文件_06


调用:



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