随笔 - 272  文章 - 7  评论 - 27  阅读 - 83万

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 = @"SOFTWARE\Wow6432Node\WinRAR";//64位注册表
            if (IntPtr.Size == 4)
            {
                openKey = @"SOFTWARE\WinRAR";//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
复制代码

 

posted on   NLazyo  阅读(426)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示