zoukankan      html  css  js  c++  java
  • 使用DOTNETZIP过滤并压缩相对目录

    业务要求:

    1. 压缩某个文件夹及其子目录
    2. 压缩时只压缩指定的文件类型,如cshtml
    3. 压缩后保持相对目录

     

    找了很久,没有直接的DEMO,最后尝试通过以下代码完成

    示例演示了只压缩cshtml和js,同时跳过debugjs和bin目录

     

     

    1. /// <summary>
    2.        ///
    3.        /// </summary>
    4.        /// <param name="args">
    5.        /// <example>
    6.        /// <code>
    7.        /// args = new string[] {
    8.        /// "ZipFile",
    9.        /// @"Path=D:kljobCardLanCardLan.Web.OneCard",
    10.        /// "Filter=*.cshtml;*.js",
    11.        /// "TargetFile=d:\temp\zip.zip" ,
    12.        /// "ZipType=DotNet",
    13.        /// "SkipPath=DebugJS;bin"
    14.        /// };
    15.        ///
    16.        /// </code>
    17.        /// </example>
    18.        /// </param>
    19.        /// <returns></returns>
    20.        public static int Zip(string[] args)
    21.        {
    22.            string path = Helper.ArgHelper.FindArg(args, "Path");
    23.            string targetFile = Helper.ArgHelper.FindArg(args, "TargetFile");
    24.            string zipType = Helper.ArgHelper.FindArg(args, "ZipType");
    25.            string filter = Helper.ArgHelper.FindArg(args, "Filter");
    26.            string skipPath = Helper.ArgHelper.FindArg(args, "SkipPath");
    27.  
    28.  
    29.            if (!System.IO.Directory.Exists(path))
    30.                throw new System.IO.DirectoryNotFoundException(path);
    31.  
    32.  
    33.            switch (zipType)
    34.            {
    35.                case "DotNet":
    36.                default:
    37.                    using (ZipFile zip = new ZipFile(System.Text.Encoding.UTF8))//设置编码,解决压缩文件时中文乱码
    38.                    {
    39.                        StringBuilder sb = new StringBuilder("");
    40.                        foreach (var item in skipPath.Split(';'))
    41.                        {
    42.                            if (!string.IsNullOrEmpty(item))
    43.                                sb.AppendFormat("name!={1}\{0}\* and ", item,path);
    44.                        }
    45.                        zip.AddSelectedFiles(sb.ToString() + " (name=" + string.Join(" or name=", filter.Split(';')) + ")", path, "", true);
    46.                        zip.Save(targetFile);
    47.                    }
    48.                    return 0;
    49.            }
    50.        }

     

    参考:

     

    http://dotnetzip.herobo.com/DNZHelp/html/547e4c24-4683-96df-036e-19bc34ba27e4.htm

    http://dotnetzip.herobo.com/DNZHelp/html/b5ca1211-94be-6039-cd07-61d3821d9c3d.htm

     

     

  • 相关阅读:
    MySQL根据出生日期计算年龄的五种方法比较
    用于测试API并生文档的开发人员工具
    【实例】使用Eolinker工具进行接口测试时传递集合参数的方法
    如何克服API测试中的挑战
    关于API网关(一)性能
    为什么需要监控API
    比Swagger2更好用的自动生成文档工具?对比流程说话!
    如何通过3个步骤执行基本的API测试
    【学习】API接口测试用例编写规则
    微信小程序之蓝牙 BLE 踩坑记录
  • 原文地址:https://www.cnblogs.com/QinQouShui/p/4447705.html
Copyright © 2011-2022 走看看