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);
            }
        }
    }
原文地址:https://www.cnblogs.com/suzixuan/p/6898341.html