zoukankan      html  css  js  c++  java
  • C#文件压缩类

    文件压缩类

    文件压缩使用:WinRAR,dotnetzip

    http://dotnetzip.codeplex.com/

    下面是代码

        public class ZipHelper
        {
            private string zipExePath = "";//C:Program FilesWinRARWinRAR.exe
    
            public ZipHelper() { }
    
            public ZipHelper(string zipPath)
            {
                if (string.IsNullOrEmpty(zipPath))
                {
                    throw new ArgumentException("winRAR程序所在目录不能为空");
                }
                this.zipExePath = zipPath;
            }
    
            public void SetZipExePath(string zipPath)
            {
                this.zipExePath = zipPath;
            }
    
            /// <summary>
            /// winRAR压缩文件夹
            /// </summary>
            /// <param name="source">文件夹地址(绝对地址)</param>
            /// <param name="dest">压缩后文件存放位置(绝对地址)</param>
            public void Zip(string source, string dest)
            {
                if (string.IsNullOrEmpty(zipExePath))
                {
                    throw new ArgumentException("请使用带参数的构造函数");
                }
    
                string parameters = String.Format("a -k -m1 -ep1 -afzip -r -o+  {0} {1}", dest, source);
    
                ProcessStartInfo psi = new ProcessStartInfo();
                psi.RedirectStandardOutput = true;
                psi.RedirectStandardError = true;
                psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                psi.UseShellExecute = false;
                psi.FileName = zipExePath;
                psi.Arguments = parameters;
                System.Diagnostics.Process process = System.Diagnostics.Process.Start(psi);
    
                int i = 0;
                do
                {
                    process.WaitForExit(2000);
                    i++;
                } while (process.HasExited == false && i < 15);
            }
    
            /// <summary>
            /// IonicZip压缩文件夹
            /// </summary>
            /// <param name="source">文件夹地址(绝对地址)</param>
            /// <param name="dest">压缩后文件存放位置(绝对地址)</param>
            public void IonicZip(string source, string dest)
            {
                using (ZipFile zip = new ZipFile(Encoding.UTF8))
                {
                    zip.AddDirectory(source, "files");
                    zip.Comment = String.Format("This zip archive was created by erichfund on machine '{0}'",
                       System.Net.Dns.GetHostName());
    
                    zip.Save(dest);
                }
            }
        }
  • 相关阅读:
    Talk the Talk
    2.1 使用eclipse4.4 搭建 maven简单结构项目。
    [LeetCode] Best Time to Buy and Sell Stock
    hdu4605Magic Ball Game 树状数组
    phoenixframe自己主动化平台在Linux环境下运行用例的说明
    数据存储值归档Archive
    BZOJ 1040 ZJOI2008 骑士 树形DP
    HDOJ 5357 Easy Sequence DP
    Autodesk 招聘Revit二次开发咨询顾问,与Autodesk全球团队紧密合作,提高职业生涯的好机会
    Codeforces Round #263 (Div. 1) A B C
  • 原文地址:https://www.cnblogs.com/suzixuan/p/6898341.html
Copyright © 2011-2022 走看看