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);
                }
            }
        }
  • 相关阅读:
    STL::next_permutation();
    P2626 斐波那契数列(升级版)
    P1029 最大公约数和最小公倍数问题
    P1075 质因数分解
    4.7生日当天测
    cin,scanf,gets,getline,cin.getline对于字符串的输入
    两个互质的数不能凑出来的数证明
    简单的全排列问题(给初学者)
    紫书 例题 10-22 UVa 1640(数位统计)
    紫书 例题 10-21 UVa 11971(连续概率)
  • 原文地址:https://www.cnblogs.com/suzixuan/p/6898341.html
Copyright © 2011-2022 走看看