业务要求:
- 压缩某个文件夹及其子目录
- 压缩时只压缩指定的文件类型,如cshtml
- 压缩后保持相对目录
?
找了很久,没有直接的DEMO,最后尝试通过以下代码完成
示例演示了只压缩cshtml和js,同时跳过debugjs和bin目录
?
?
- /// <summary>
				
- ???????/// 
				
- ???????/// </summary>
				
- ???????/// <param name="args">
				
- ???????/// <example>
				
- ???????/// <code>
				
- ???????/// args = new string[] { 
				
- ???????/// "ZipFile", 
				
- ???????/// @"Path=D:\kljob\CardLan\CardLan.Web.OneCard", 
				
- ???????/// "Filter=*.cshtml;*.js", 
				
- ???????/// "TargetFile=d:\\temp\\zip.zip" ,
				
- ???????/// "ZipType=DotNet",
				
- ???????/// "SkipPath=DebugJS;bin" 
				
- ???????/// };
				
- ???????/// 
				
- ???????/// </code>
				
- ???????/// </example>
				
- ???????/// </param>
				
- ???????/// <returns></returns>
				
- ???????public
					static
					int Zip(string[] args)
- ???????{
- ???????????string path = Helper.ArgHelper.FindArg(args, "Path");
- ???????????string targetFile = Helper.ArgHelper.FindArg(args, "TargetFile");
- ???????????string zipType = Helper.ArgHelper.FindArg(args, "ZipType");
- ???????????string filter = Helper.ArgHelper.FindArg(args, "Filter");
- ???????????string skipPath = Helper.ArgHelper.FindArg(args, "SkipPath");
- 
?
- 
?
- ???????????if (!System.IO.Directory.Exists(path))
- ???????????????throw
					new System.IO.DirectoryNotFoundException(path);
- 
?
- 
?
- ???????????switch (zipType)
- ???????????{
- ???????????????case "DotNet":
- ???????????????default:
- ???????????????????using (ZipFile zip = new ZipFile(System.Text.Encoding.UTF8))//设置编码,解决压缩文件时中文乱码
				
- ???????????????????{
- ???????????????????????StringBuilder sb = new StringBuilder("");
- ???????????????????????foreach (var item in skipPath.Split(';'))
- ???????????????????????{
- ???????????????????????????if (!string.IsNullOrEmpty(item))
- ???????????????????????????????sb.AppendFormat("name!=*\\{0}\\* and ", item);
- ???????????????????????}
- ???????????????????????zip.AddSelectedFiles(sb.ToString() + " (name=" + string.Join(" or name=", filter.Split(';')) + ")", path, "", true);
- ???????????????????????zip.Save(targetFile);
- ???????????????????}
- ???????????????????return 0;
- ???????????}
- ???????}
?
参考:
?
http://dotnetzip.herobo.com/DNZHelp/html/547e4c24-4683-96df-036e-19bc34ba27e4.htm
	
http://dotnetzip.herobo.com/DNZHelp/html/b5ca1211-94be-6039-cd07-61d3821d9c3d.htm
	
?
?