C# 调用WinRAR解压缩文件

Deadly 提交于 2019-12-05 06:18:01

在项目中有可能会用到解压缩,代码如下

public class CompressHelper
    {
        
        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="sourceFilePath">源文件路径</param>
        /// <param name="sourceName">源文件名称</param>
        /// <param name="compressName">压缩文件名</param>
        /// <param name="compressPath">压缩文件路径</param>
        /// <returns></returns>
        public static bool CompressFile(string sourceFilePath, string sourceName, string compressName, string compressPath)
        {
            string winrarPath = string.Empty;
            if (!ExistsCompressApp(out winrarPath))
            {
                return false;
            }
            if (string.IsNullOrEmpty(compressPath))
            {
                compressPath = sourceFilePath;
            }
            string type = compressName.Substring(compressName.IndexOf('.') + 1);
            string compressType = string.Empty;
            if (type.ToUpper() == CompressType.RAR.ToString())
            {
                compressType = "-af" + CompressType.RAR.ToString().ToLower();
            }
            else if (type.ToUpper() == CompressType.ZIP.ToString())
            {
                compressType = "-af" + CompressType.ZIP.ToString().ToLower();
            }
            else
            {
                compressType = "-af" + CompressType.RAR.ToString().ToLower();
                compressName = compressName + "." + CompressType.RAR.ToString().ToLower();
            }
            CreateFolder(compressPath);
            /**
             * <命令> -<开关1> -<开关N> <压缩文件 > <文件...> <@列表文件...> <解压路径\> 
             * 压缩 a      a -ep -o+ -df d:\test.rar d:\test.jpg
             * 压缩 -ep(排除文件名的路径) -o+(覆盖全部文件 (更新压缩文件时的默认值))
             *      -df(压缩后删除源文件)-afrar(-AF<类型> - 指定压缩文件格式)
             * */
            string cmd = string.Format(@" a -ep -o+ {2} -df {0} {1}", Path.Combine(compressPath, compressName), Path.Combine(sourceFilePath, sourceName), compressType);
            return StartProcess(winrarPath, cmd);
        }

        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="unCompressPath">解压文件路径</param>
        /// <param name="unCompressName">解压文件名</param>
        /// <param name="filePath">解压后文件路径</param>
        /// <returns></returns>
        public static bool UnCompressFile(string unCompressPath, string unCompressName, string filePath)
        {
            string winrarPath = string.Empty;
            if (!ExistsCompressApp(out winrarPath))
            {
                return false;
            }
            if (string.IsNullOrEmpty(filePath))
            {
                filePath = unCompressPath;
            }
            CreateFolder(filePath);
            /**
             * <命令> -<开关1> -<开关N> <压缩文件 > <文件...> <@列表文件...> <解压路径\> 
             * 解压 x      x -ibck -inul -y -mt5 d:\test.rar d:\test
             * 解压 -ibck(在后台执行 WinRAR) -inul(禁用错误信息)  -y(所有问题的默认值为“是”)
             *      -mt5(设置线程数量)
             * */
            string cmd = string.Format(@" x -ibck -inul -y -mt5 {0} {1}", Path.Combine(unCompressPath, unCompressName), filePath);
            return StartProcess(winrarPath, cmd);
        }

        /// <summary>
        /// 判断是否安装了压缩软件
        /// </summary>
        /// <param name="winrarPath">软件安装路径</param>
        /// <returns></returns>
        private static bool ExistsCompressApp(out string winrarPath)
        {
            winrarPath = string.Empty;
            //通过注册表找到WinRAR软件
            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
            if (key == null)
                return false;
            winrarPath = key.GetValue("").ToString();//获取winrar软件安装路径
            key.Close(); //关闭注册表
            return !string.IsNullOrEmpty(winrarPath);
        }

        /// <summary>
        /// 执行压缩操作
        /// </summary>
        /// <param name="exePath">WinRAR路径</param>
        /// <param name="exeArg">WinRAR参数</param>
        /// <param name="isHide">是否显示窗体</param>
        /// <returns></returns>
        private static bool StartProcess(string exePath, string exeArg, bool isHide = false)
        {
            Process p = new Process();
            p.StartInfo.FileName = exePath;
            p.StartInfo.Arguments = exeArg;
            if (isHide)
            {
                p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                p.StartInfo.CreateNoWindow = true;
            }
            else
            {
                p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                p.StartInfo.CreateNoWindow = false;
            }
            p.Start();
            int idx = 1;
            while (!p.HasExited)
            {
                idx++;
                p.WaitForExit(1000);
                if (idx == 50)
                {
                    p.Kill();
                    return false;
                }
            }
            p.Close();
            p.Dispose();
            return true;
        }

        /// <summary>
        /// 判断文件夹是否存在
        /// </summary>
        /// <param name="path">路径</param>
        private static void CreateFolder(string path)
        {
            if (!File.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }
    }

    /// <summary>
    /// 压缩类型
    /// </summary>
    public enum CompressType
    { 
        RAR,
        ZIP
    }
View Code

具体的命令可以参考winrar的帮助文档

 

 参考 https://www.cnblogs.com/zhoub/p/3841276.html

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!