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);
                }
            }
        }
  • 相关阅读:
    Qt 定时器事件startTimer
    认识网络、几种常用的网络拓扑图
    拓扑结构图,什么是拓扑结构
    Qt 利用QTime类来控制时间,QTime的成员函数的用法
    Qt QTime类的使用
    Qt 打开文件的默认路径 QFileDialog::getOpenFileName()
    Qt QWidget颜色设置的三种方法
    Qt 多个QDockWidget 切换显示
    Qt QString 格式化 arg 前面自动补0
    Qt 使用QMediaPlayer报错 defaultServiceProvider::requestService(): no service found for
  • 原文地址:https://www.cnblogs.com/suzixuan/p/6898341.html
Copyright © 2011-2022 走看看