如何用C#+WinRAR 实现压缩

最近经常下载一些小朋友的资源,发现很多都是压缩文件, 几百个, 于是写程序解压。

using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine(ExistsWinRar());
            string dirPath = @"E:\BaiduNetdiskDownload\02 - Super Simple Songs 音频242首MP3";

          var files =  Directory.GetFiles(dirPath, "*.rar");
            foreach (var file in files)
            {
                UnRAR(file, dirPath);
                File.Delete(file);
            }

        }
        /// <summary>
        /// 获取WinRAR.exe路径
        /// </summary>
        /// <returns>为空则表示未安装WinRAR</returns>
        public static string ExistsRAR()
        {
            RegistryKey regkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
            //RegistryKey regkey = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\shell\open\command");
            string strkey = regkey.GetValue("").ToString();
            regkey.Close();
            //return strkey.Substring(1, strkey.Length - 7);
            return strkey;
        }

        /// <summary>
        /// 解压RAR文件
        /// </summary>
        /// <param name="rarFilePath">要解压的文件路径</param>
        /// <param name="unrarDestPath">解压路径(绝对路径)</param>
        public static void UnRAR(string rarFilePath, string unrarDestPath)
        {
            string rarexe = ExistsRAR();
            if (String.IsNullOrEmpty(rarexe))
            {
                throw new Exception("未安装WinRAR程序。");
            }
            try
            {
                //组合出需要shell的完整格式
                string shellArguments = string.Format("x -o+ \"{0}\" \"{1}\\\"", rarFilePath, unrarDestPath);

                //用Process调用
                using (Process unrar = new Process())
                {
                    ProcessStartInfo startinfo = new ProcessStartInfo();
                    startinfo.FileName = rarexe;
                    startinfo.Arguments = shellArguments;               //设置命令参数
                    startinfo.WindowStyle = ProcessWindowStyle.Hidden;  //隐藏 WinRAR 窗口

                    unrar.StartInfo = startinfo;
                    unrar.Start();
                    unrar.WaitForExit();//等待解压完成

                    unrar.Close();
                    
                }
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        ///  压缩为RAR文件
        /// </summary>
        /// <param name="filePath">要压缩的文件路径(绝对路径)</param>
        /// <param name="rarfilePath">压缩到的路径(绝对路径)</param>
        public static void RAR(string filePath, string rarfilePath, string otherPara)
        {
            RAR(filePath, rarfilePath, "", "", otherPara);
        }

        /// <summary>
        ///  压缩为RAR文件
        /// </summary>
        /// <param name="filePath">要压缩的文件路径(绝对路径)</param>
        /// <param name="rarfilePath">压缩到的路径(绝对路径)</param>
        /// <param name="rarName">压缩后压缩包名称</param>
        public static void RAR(string filePath, string rarfilePath, string rarName, string otherPara)
        {
            RAR(filePath, rarfilePath, rarName, "", otherPara);
        }

        /// <summary>
        ///  压缩为RAR文件
        /// </summary>
        /// <param name="filePath">要压缩的文件路径(绝对路径)</param>
        /// <param name="rarfilePath">压缩到的路径(绝对路径)</param>
        /// <param name="rarName">压缩后压缩包名称</param>
        /// <param name="password">解压密钥</param>
        public static void RAR(string filePath, string rarfilePath, string rarName, string password, string otherPara)
        {
            string rarexe = ExistsRAR();
            if (String.IsNullOrEmpty(rarexe))
            {
                throw new Exception("未安装WinRAR程序。");
            }

            if (!Directory.Exists(filePath))
            {
                //throw new Exception("文件不存在!");
            }

            if (String.IsNullOrEmpty(rarName))
            {
                rarName = Path.GetFileNameWithoutExtension(filePath) + ".rar";
            }
            else
            {
                if (Path.GetExtension(rarName).ToLower() != ".rar")
                {
                    rarName += ".rar";
                }
            }

            try
            {
                //Directory.CreateDirectory(rarfilePath);
                //压缩命令,相当于在要压缩的文件夹(path)上点右键->WinRAR->添加到压缩文件->输入压缩文件名(rarName)
                string shellArguments;
                if (String.IsNullOrEmpty(password))
                {
                    shellArguments = string.Format("a -ep1 \"{0}\" \"{1}\" -r", rarName, filePath);
                }
                else
                {
                    shellArguments = string.Format("a -ep1 \"{0}\" \"{1}\" -r -p\"{2}\"", rarName, filePath, password);
                }
                if (!string.IsNullOrEmpty(otherPara))
                {
                    shellArguments = shellArguments + " " + otherPara;
                }

                using (Process rar = new Process())
                {
                    ProcessStartInfo startinfo = new ProcessStartInfo();
                    startinfo.FileName = rarexe;
                    startinfo.Arguments = shellArguments;               //设置命令参数
                    startinfo.WindowStyle = ProcessWindowStyle.Hidden;  //隐藏 WinRAR 窗口
                    startinfo.WorkingDirectory = rarfilePath;

                    rar.StartInfo = startinfo;
                    rar.Start();
                    rar.WaitForExit(); //无限期等待进程 winrar.exe 退出
                    rar.Close();
                }
            }
            catch
            {
                throw;
            }
        }

    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值