zoukankan      html  css  js  c++  java
  • 调用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;
            }
        }

  • 相关阅读:
    计算机网络基础,子网掩码,网络号,子网号,主机号主机数量计算方式
    Nginx配置大全与搭建手册
    Windows10仿mac-os主题
    kali-linux知识整理与渗透测试指南
    简单的钓鱼网站制作-Setoolkit
    渗透测试常用工具-Metasploit_常用模块
    提权方式及原理简介(面试)
    修改dedecms精简版
    内网存活主机探测的一些方法
    免杀测试
  • 原文地址:https://www.cnblogs.com/lijinchang/p/2235876.html
Copyright © 2011-2022 走看看