zoukankan      html  css  js  c++  java
  • WinRAR压缩操作帮助类

    //-------------------------------------------------------------------------------------
    // All Rights Reserved , Copyright (C) 2016 , ZTO , Ltd .
    //-------------------------------------------------------------------------------------
    
    using System;
    using System.Diagnostics;
    using System.IO;
    using Microsoft.Win32;
    
    namespace ZTO.TestDb
    {
        /// <summary>
        /// WinRAR压缩操作帮助类
        ///
        /// 修改纪录
        ///
        ///          2016-5-9  版本:1.0 YangHengLian 创建主键,注意命名空间的排序,测试非常好。
        ///       2016-5-13 加上了自动获取WinRAR安装路径的函数,不管是64位还是32位系统,
        /// 
        /// 版本:1.0
        ///
        /// <author>
        ///        <name>YangHengLian</name>
        ///        <date>2016-5-9</date>
        /// </author>
        /// </summary>
        public class WinRarHelper
        {
            #region 属性
            private string _winRarPath;
    
            /// <summary>
            /// WinRAR安装路径,可以自己设置,默认读取系统注册表
            /// </summary>
            public string WinRarPath
            {
                get
                {
                    return string.IsNullOrEmpty(_winRarPath) ? GetWinRarInstallPath() : _winRarPath;
                }
                set
                {
                    _winRarPath = value;
                }
            }
            #endregion
    
            /// <summary>
            /// 解压到某个文件夹中
            /// </summary>
            /// <param name="rarFilePath">rar文件全路径</param>
            /// <param name="unRarPath">解压到哪个文件夹</param>
            /// <param name="password">解压密码</param>
            /// <param name="isOverride">是否覆盖</param>
            public void UnRar(string rarFilePath, string unRarPath, string password = null, bool isOverride = false)
            {
                if (IsSetUpWinRar())
                {
                    throw new ArgumentNullException("WinRAR未安装");
                }
                RunCmd(string.Format("x{0} -o{1} {2} {3}", (password == null ? "" : " -p" + password), (isOverride ? "+" : "-"), rarFilePath, unRarPath));
            }
    
            /// <summary>
            /// 压缩文件或者文件夹为压缩包
            /// </summary>
            /// <param name="filePath">需要压缩的文件/文件夹全路径</param>
            /// <param name="saveFilePath">压缩文件保存全路径</param>
            /// <param name="isOverride">是否覆盖</param>
            /// <param name="password">压缩文件密码</param>
            public void Rar(string filePath, string saveFilePath, bool isOverride = false, string password = null)
            {
                if (IsSetUpWinRar())
                {
                    throw new ArgumentNullException("WinRAR未安装");
                }
                RunCmd(string.Format("a{0} -o{1} -ep2 -r {2} {3}", (password == null ? "" : " -p" + password), (isOverride ? "+" : "-"), saveFilePath, filePath));
            }
    
            /// <summary>
            /// 解压是否安装了WinRAR程序
            /// </summary>
            /// <returns></returns>
            public bool IsSetUpWinRar()
            {
                if (!string.IsNullOrEmpty(WinRarPath))
                {
                    return File.Exists(WinRarPath);
                }
                var inistallPath = GetWinRarInstallPath();
                if (string.IsNullOrEmpty(inistallPath))
                {
                    return false;
                }
                WinRarPath = inistallPath;
                return true;
            }
    
            /// <summary>
            /// 从注册表中获取WinRAR的安装路径
            /// </summary>
            /// <returns></returns>
            public string GetWinRarInstallPath()
            {
                var openKey = @"SOFTWAREWow6432NodeWinRAR";//64位注册表
                if (IntPtr.Size == 4)
                {
                    openKey = @"SOFTWAREWinRAR";//32位注册表路径
                }
                RegistryKey appPath = Registry.LocalMachine.OpenSubKey(openKey);
                if (appPath != null)
                {
                    // WinRAR安装具体路径
                    string path = appPath.GetValue("exe32").ToString();
                    if (File.Exists(path))
                    {
                        return path;
                    }
                }
                return null;
            }
    
            /// <summary>
            /// 执行rar内部命令
            /// </summary>
            /// <param name="cmd">要执行的命令</param>
            public void RunCmd(string cmd)
            {
                using (var p = new Process())
                {
                    p.StartInfo.FileName = WinRarPath;
                    p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    p.StartInfo.Arguments = cmd;
                    p.Start();
                    p.WaitForExit();
                }
            }
        }
    }
    
    WinRarHelper
  • 相关阅读:
    SQL 分组 行变列的一个例子
    用JS如何獲得DropDownList所選Text和Value?
    用了.net2.0,再用1.1的问题。1.1里修改.cs文件不重新编译,.dll不重新生成。
    ASP.NET中上传文件
    获得用户控件的值!
    在没有vs2005环境里部署Crystal Reports 10水晶报表
    onkeypress,onkeydown,onkeyup区别
    2012湖南大学第八届程序设计竞赛 Incredible[公式]
    POJ3624 Charm Bracelet[01背包问题入门]
    HDOJ1257 最少拦截系统[DP入门]
  • 原文地址:https://www.cnblogs.com/bile/p/5497440.html
Copyright © 2011-2022 走看看