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);
                }
            }
        }
  • 相关阅读:
    shell脚本 加密备份MySQL数据库
    C#在Linux下获取文件夹信息(所在磁盘总大小,使用空间,已用空间,使用率)
    bootstrap--- 两种bootstrap multiselect组件大比拼
    C# 文件重命名
    C#中一些常用的正则表达式
    C# 文件压缩加解密
    Python 由__dict__和dir()引发的一些思考
    python3随机生成中文字符
    Django自定义过滤器中is_safe和need_autoescape两个参数的理解
    Python格式化字符串--format
  • 原文地址:https://www.cnblogs.com/suzixuan/p/6898341.html
Copyright © 2011-2022 走看看