C#解压、压缩RAR文件

摘要:
usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.IO;usingMicrosoft.Win32;usingSystem.Diagnostics;namespaceCommonHelper{publicclassWinRARHelper{//////是否安装了Winrar///˂/su
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.IO;
usingMicrosoft.Win32;
usingSystem.Diagnostics;

namespaceCommonHelper
{
    public classWinRARHelper
    {
        /// <summary>
        ///是否安装了Winrar
        /// </summary>
        /// <returns></returns>
        static public boolExists()
        {
            RegistryKey the_Reg =Registry.LocalMachine.OpenSubKey(
                    @"SOFTWAREMicrosoftWindowsCurrentVersionApp PathsWinRAR.exe");
            return !string.IsNullOrEmpty(the_Reg.GetValue("").ToString());
        }

        /// <summary>
        ///打包成Rar
        /// </summary>
        /// <param name="patch"></param>
        /// <param name="rarPatch"></param>
        /// <param name="rarName"></param>
        public void CompressRAR(string patch, string rarPatch, stringrarName)
        {
            stringthe_rar;
            RegistryKey the_Reg;
            objectthe_Obj;
            stringthe_Info;
            ProcessStartInfo the_StartInfo;
            Process the_Process;
            try{
                the_Reg =Registry.LocalMachine.OpenSubKey(
                         @"SOFTWAREMicrosoftWindowsCurrentVersionApp PathsWinRAR.exe");
                the_Obj = the_Reg.GetValue("");
                the_rar =the_Obj.ToString();
                the_Reg.Close();
                the_rar = the_rar.Substring(1, the_rar.Length - 7);
                Directory.CreateDirectory(patch);
                //命令参数
                //the_Info = " a    " + rarName + " " + @"C:Test?70821.txt"; //文件压缩
                the_Info = "a    " + rarName + " " + patch + "-r"; ;
                the_StartInfo = newProcessStartInfo();
                the_StartInfo.FileName =the_rar;
                the_StartInfo.Arguments =the_Info;
                the_StartInfo.WindowStyle =ProcessWindowStyle.Hidden;
                //打包文件存放目录
                the_StartInfo.WorkingDirectory =rarPatch;
                the_Process = newProcess();
                the_Process.StartInfo =the_StartInfo;
                the_Process.Start();
                the_Process.WaitForExit();
                the_Process.Close();
            }
            catch(Exception ex)
            {
                throwex;
            }
        }
        /// <summary>
        ///解压
        /// </summary>
        /// <param name="unRarPatch"></param>
        /// <param name="rarPatch"></param>
        /// <param name="rarName"></param>
        /// <returns></returns>
        public string unCompressRAR(string unRarPatch, string rarPatch, stringrarName)
        {
            stringthe_rar;
            RegistryKey the_Reg;
            objectthe_Obj;
            stringthe_Info;

            try{
                the_Reg =Registry.LocalMachine.OpenSubKey(
                         @"SOFTWAREMicrosoftWindowsCurrentVersionApp PathsWinRAR.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(unRarPatch) == false)
                {
                    Directory.CreateDirectory(unRarPatch);
                }
                the_Info = "x " + rarName + " " + unRarPatch + "-y";

                ProcessStartInfo the_StartInfo = newProcessStartInfo();
                the_StartInfo.FileName =the_rar;
                the_StartInfo.Arguments =the_Info;
                the_StartInfo.WindowStyle =ProcessWindowStyle.Hidden;
                the_StartInfo.WorkingDirectory = rarPatch;//获取压缩包路径
Process the_Process = newProcess();
                the_Process.StartInfo =the_StartInfo;
                the_Process.Start();
                the_Process.WaitForExit();
                the_Process.Close();
            }
            catch(Exception ex)
            {
                throwex;
            }
            returnunRarPatch;
        }
    }
}

//RAR参数:
 
//一、压缩命令
//1、将temp.txt压缩为temp.rarrar a temp.rar temp.txt
//2、将当前目录下所有文件压缩到temp.rarrar a temp.rar *.*
//3、将当前目录下所有文件及其所有子目录压缩到temp.rarrar a temp.rar *.* -r
//4、将当前目录下所有文件及其所有子目录压缩到temp.rar,并加上密码123rar a temp.rar *.* -r -p123
 
//二、解压命令
//1、将temp.rar解压到c:	emp目录rar e temp.rar c:	emprar e *.rar c:	emp(支持批量操作)
//2、将temp.rar解压到c:	emp目录,并且解压后的目录结构和temp.rar中的目录结构一
 
 
//压缩目录test及其子目录的文件内容
//Wzzip test.zip test -r -P
//WINRAR A test.rar test -r
 
//删除压缩包中的*.txt文件
//Wzzip test.zip *.txt -d
//WinRAR d test.rar *.txt
 
 
//刷新压缩包中的文件,即添加已经存在于压缩包中但更新的文件
//Wzzip test.zip test -f
//Winrar f test.rar test
 
 
//更新压缩包中的文件,即添加已经存在于压缩包中但更新的文件以及新文件
//Wzzip test.zip test -u
//Winrar u test.rar test
 
 
//移动文件到压缩包,即添加文件到压缩包后再删除被压缩的文件
//Wzzip test.zip -r -P -m
//Winrar m test.rar test -r
 
 
//添加全部 *.exe 文件到压缩文件,但排除有 a或b
//开头名称的文件
//Wzzip test *.exe -xf*.* -xb*.*
//WinRAR a test *.exe -xf*.* -xb*.*
 
 
//加密码进行压缩
//Wzzip test.zip test
//-s123。注意密码是大小写敏感的。在图形界面下打开带密码的压缩文件,会看到+号标记(附图1)。
//WINRAR A test.rar test -p123
//-r。注意密码是大小写敏感的。在图形界面下打开带密码的压缩文件,会看到*号标记(附图2)。
 
 
//按名字排序、以简要方式列表显示压缩包文件
//Wzzip test.zip -vbn
//Rar l test.rar
 
 
//锁定压缩包,即防止未来对压缩包的任何修改
//无对应命令
//Winrar k test.rar
 
 
//创建360kb大小的分卷压缩包
//无对应命令
//Winrar a -v360 test
 
 
//带子目录信息解压缩文件
//Wzunzip test -d
//Winrar x test -r
 
 
//不带子目录信息解压缩文件
//Wzunzip test
//Winrar e test
 
 
//解压缩文件到指定目录,如果目录不存在,自动创建
//Wzunzip test newfolder
//Winrar x test newfolder
 
 
//解压缩文件并确认覆盖文件
//Wzunzip test -y
//Winrar x test -y
 
 
//解压缩特定文件
//Wzunzip test *.txt
//Winrar x test *.txt
 
 
//解压缩现有文件的更新文件
//Wzunzip test -f
//Winrar x test -f
 
 
//解压缩现有文件的更新文件及新文件
//Wzunzip test -n
//Winrar x test -u
 
 
//批量解压缩文件
//Wzunzip *.zip
//WinRAR e *.rar

免责声明:文章转载自《C#解压、压缩RAR文件》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇利用pyinstaller打包加密Python项目golang socket编程,实现http协议下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

递归调用解压zip包或rar包

不知道有没有小伙伴跟我一样犯这样的懒,一些简单重复的工作,总是嫌麻烦,懒得一步步去做,还每次都重复一样的操作。比如解压zip或rar的包,也许你会说,不就解压嘛,有啥的。问题来了,如果只是一个简单的压缩包也是算了,关键是压缩包里还有压缩包,也不知道多少层,要一层层去解压,这就比较恶心了吧?如下图: 学习了Python的递归调用后,心想,为什么我不写一个脚...

Java 代码实现rar解压最全攻略操作

最全Java 代码实现rar解压操作首先,非常感谢下面几位链接上的支持,之所以写这篇博文,主要在于总结,同时给第一次实现该功能的同学提供完整的参考。因为第一次遇到需要在代码中实现rar和zip的解压操作。而zip其实很简单,jdk自带的ZipUtil就可以实现,这里不做赘述。但是rar的解压,特别是5.0及其以上版本的解压,折腾了我很久。根据这几位博主的思...

(6)centos安装和解压

一、rpm包安装方式步骤:1、找到相应的软件包,比如soft.version.rpm,下载到本机某个目录;2、打开一个终端,su -成root用户;3、cd soft.version.rpm所在的目录;4、输入rpm -ivh soft.version.rpm二、deb包安装方式步骤:1、找到相应的软件包,比如soft.version.deb,下载到本机某...

PowerDesigner12.5 下载、汉化及破解

一、下载   1、PowerDesigner 12.5官方下载地址  http://download.sybase.com/eval/PowerDesigner/powerdesigner125_eval.exe   大小125 MB (132,006,349 字节)    2、汉化用文 件,直接覆盖到安装目录  pdshell12.part01.rar ...

Linux下解压缩文件命令总结

---------------------------------------------tar命令: -v 可视化-c 新建包-f 指定文件名(除非你用默认用户名)-x 解压target.tar-r 增加file文件到target.tar-t 列出target.tar中的文件-u 更新target.tar中的file文件-z 调用gzip-j 调用bzi...

常见压缩格式的区别,及 Linux 下的压缩相关指令

可先浏览加粗部分 一、常见压缩档 *.zip | zip 程序压缩打包的档案; (很常见,但是因为不包含文档名编码信息,跨平台可能会乱码)*.rar | rar 程序压缩打包的档案;(在windows上很常见,但是是商业软件。)*.gz | gzip 程序压缩的档案; (linux目前使用最广泛的压缩格式)*.bz2 | bzip2 程序压缩的档案;*.x...