调用winrar压缩解压缩文件

public class WinRARManager
    {
        /// <summary>
        /// 是否安装了Winrar
        /// </summary>
        /// <returns></returns>
        static public bool Exists()
        {
            bool haveWinRAR = true;
            RegistryKey the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
            if (the_Reg == null)
            {
                haveWinRAR = false;
            }
            else
            {
                if (string.IsNullOrEmpty(the_Reg.GetValue("").ToString()))
                {
                    haveWinRAR = false;
                }
            }
            return haveWinRAR;
        }

        /// <summary>
        /// 路径加引号(当路径中有空格时会导致调用WinRAR压缩或解压函数失败,给路径加上引号可以解决。)
        /// </summary>
        /// <param name="path">路径</param>
        /// <returns>加上引号后的路径</returns>
        private static string GetPath(string path)
        {
            return "\"" + path + "\"";
        }

        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="filePath">要压缩的文件所在目录或要压缩的文件名(带路径)</param>
        /// <param name="rarPath">压缩文件文件名(含路径)</param>
        /// <param name="isSavePath">是否存储文件路径</param>
        /// <returns>压缩是否成功</returns>
        public static bool CompressRAR(string filePath, string rarPath, bool isSavePath)
        {          
            bool success = false;
            string the_rar;
            RegistryKey the_Reg;
            object the_Obj;
            string the_Info;
            Process the_Process = new Process();
            string cmd = "";
            try
            {
                the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
                the_Obj = the_Reg.GetValue("");
                the_rar = the_Obj.ToString();
                the_Reg.Close();
                string directory = rarPath.Substring(0, rarPath.LastIndexOf("\\"));
                if (!string.IsNullOrEmpty(directory))
                {
                    if (!Directory.Exists(directory))
                    {
                        Directory.CreateDirectory(rarPath);
                    }
                }
                filePath = GetPath(filePath);
                rarPath = GetPath(rarPath);
                //命令参数
                //the_Info = " a    " + rarName + " " + @"C:Test?70821.txt"; //文件压缩
                if (isSavePath)
                {
                    cmd = "a -r ";

                }
                else
                {
                    cmd = "a -ep ";
                }
                the_Info = cmd + rarPath + " " + filePath;

                //打包文件存放目录
                //the_StartInfo.WorkingDirectory = rarPath;
           
                the_Process.StartInfo.FileName = the_rar;
                the_Process.StartInfo.Arguments = the_Info;
                the_Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                the_Process.Start();
                the_Process.WaitForExit();
                if (the_Process.HasExited)
                {
                    int iExitCode = the_Process.ExitCode;
                    if (iExitCode == 0)
                    {
                        //正常完成
                        Console.WriteLine("成功");
                        success = true;
                    }
                    else
                    {
                        //有错
                        Console.WriteLine("失败");
                        success = false;
                    }
                }

            }
            catch
            {
                success = false;
            }
            finally
            {
                the_Process.Close();
            }
            return success;
        }

        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="filePath">要压缩的文件所在目录</param>
        /// <param name="rarPath">压缩文件存放目录</param>
        /// <param name="rarName">压缩文件名(不含路径)</param>
        /// <returns>压缩是否成功</returns>
        private bool CompressRAR(string filePath, string rarPath, string rarName)
        {
          
            bool success = false;
            string the_rar;
            RegistryKey the_Reg;
            object the_Obj;
            string the_Info;
            ProcessStartInfo the_StartInfo;
            Process the_Process;
            try
            {
                the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
                the_Obj = the_Reg.GetValue("");
                the_rar = the_Obj.ToString();
                the_Reg.Close();
                //the_rar = the_rar.Substring(1, the_rar.Length - 7);
                if (!Directory.Exists(rarPath))
                {
                    Directory.CreateDirectory(rarPath);
                }
                filePath = GetPath(filePath);
                rarPath = GetPath(rarPath);
                //命令参数
                //the_Info = " a    " + rarName + " " + @"C:Test?70821.txt"; //文件压缩
                the_Info = "a -r" + rarName + " " + filePath;
                the_StartInfo = new ProcessStartInfo();
                //the_StartInfo.FileName = the_rar;
                the_StartInfo.FileName = the_rar;
                the_StartInfo.Arguments = the_Info;
                the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                //打包文件存放目录
                the_StartInfo.WorkingDirectory = rarPath;
                the_Process = new Process();
                the_Process.StartInfo = the_StartInfo;
                the_Process.Start();
                the_Process.WaitForExit();
                if (the_Process.HasExited)
                {
                    int iExitCode = the_Process.ExitCode;
                    if (iExitCode == 0)
                    {
                        //正常完成
                        Console.WriteLine("成功");
                        success = true;
                    }
                    else
                    {
                        //有错
                        Console.WriteLine("失败");
                        success = false;
                    }
                }
                the_Process.Close();
            }
            catch
            {
                success = false;
            }
            return success;
        }

        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="unRarPath">解压后文件存放目录</param>
        /// <param name="rarPath">要解压的压缩文件名(含路径)</param>
        /// <param name="includeUnCompressPath">解压时是否包含路径</param>
        /// <param name="isTip">覆盖文件时是否提示</param>
        /// <returns>解压是否成功</returns>
        public static bool UnCompressRAR(string unRarPath, string rarPath, bool includeUnCompressPath, bool isTip)
        {
           
            bool success = false;
            string the_rar;
            RegistryKey the_Reg;
            object the_Obj;
            string the_Info;
            Process the_Process = new Process();
            string cmd = "";
            try
            {
                the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
                the_Obj = the_Reg.GetValue("");
                the_rar = the_Obj.ToString();
                the_Reg.Close();

                if (!Directory.Exists(unRarPath))
                {
                    Directory.CreateDirectory(unRarPath);
                }
                unRarPath = GetPath(unRarPath);
                rarPath = GetPath(rarPath);
                if (includeUnCompressPath)
                {
                    if (isTip)
                    {
                        cmd = "x ";
                    }
                    else
                    {
                        cmd = "x -y ";
                    }

                }
                else
                {
                    if (isTip)
                    {
                        cmd = "e ";
                    }
                    else
                    {
                        cmd = "e -y ";
                    }
                }
                the_Info = cmd + rarPath + " " + unRarPath;


                //the_StartInfo.WorkingDirectory = rarPath;//获取压缩包路径
               
                the_Process.StartInfo.FileName = the_rar;
                the_Process.StartInfo.Arguments = the_Info;
                the_Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                the_Process.Start();
                the_Process.WaitForExit();
                if (the_Process.HasExited)
                {
                    int iExitCode = the_Process.ExitCode;
                   
                    if (iExitCode == 0)
                    {
                        //正常完成
                        Console.WriteLine("成功");
                        success = true;
                    }
                    else
                    {
                        //有错
                        Console.WriteLine("失败");
                        success = false;
                    }
                }

            }
            catch
            {
                success = false;
            }
            finally
            {
                the_Process.Close();
            }
            return success;

        }

        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="unRarPath">解压后文件存放目录</param>
        /// <param name="rarPath">要解压的压缩文件所在目录</param>
        /// <param name="rarName">压缩文件名</param>
        /// <returns>解压是否成功</returns>
        private bool UnCompressRAR(string unRarPath, string rarPath, string rarName)
        {
           
            bool success = false;
            string the_rar;
            RegistryKey the_Reg;
            object the_Obj;
            string the_Info;
            try
            {
                the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
                the_Obj = the_Reg.GetValue("");
                the_rar = the_Obj.ToString();
                the_Reg.Close();
                //the_rar = the_rar.Substring(1, the_rar.Length - 7);

                if (Directory.Exists(unRarPath) == false)
                {
                    Directory.CreateDirectory(unRarPath);
                }
                unRarPath = GetPath(unRarPath);
                rarPath = GetPath(rarPath);
                the_Info = "x -y" + rarName + " " + unRarPath;

                ProcessStartInfo the_StartInfo = new ProcessStartInfo();
                the_StartInfo.FileName = the_rar;
                the_StartInfo.Arguments = the_Info;
                the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                the_StartInfo.WorkingDirectory = rarPath;//获取压缩包路径

                Process the_Process = new Process();
                the_Process.StartInfo = the_StartInfo;
                the_Process.Start();
                the_Process.WaitForExit();
                if (the_Process.HasExited)
                {
                    int iExitCode = the_Process.ExitCode;
                    if (iExitCode == 0)
                    {
                        //正常完成
                        Console.WriteLine("成功");
                        success = true;
                    }
                    else
                    {
                        //有错
                        Console.WriteLine("失败");
                        success = false;
                    }
                }
                the_Process.Close();
            }
            catch
            {
                success = false;
            }

            return success;
        }
    }

转载于:https://www.cnblogs.com/lijinchang/archive/2011/11/04/2235876.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值