C#调用RAR压缩与解压

public void RARsave(string rarPatch, string rarFiles,string  patch,string rarName)
        {
            String the_rar;
            RegistryKey the_Reg;
            Object the_Obj;
            String the_Info;
            ProcessStartInfo the_StartInfo;
            Process the_Process;
            try
            {
                the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications\\WinRAR.exe\\Shell\\Open\\Command");
                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(patch))
                Directory.CreateDirectory(patch);
                //命令参数

//the_Info = " a  c:\\test.rar a.txt -r"; //文件压缩

the_Info = string.Format(" a -df {0}\\{1}  {2}  -r", patch, rarName, rarFiles);// " a " + rarName + " " + patch;
                the_StartInfo = new ProcessStartInfo();
                the_StartInfo.FileName = the_rar;
                the_StartInfo.Arguments = the_Info;
                the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                //打包文件存放目录

the_StartInfo.WorkingDirectory = rarPatch;
                the_Process = new Process();
                the_Process.StartInfo = the_StartInfo;
                the_Process.Start();
                the_Process.WaitForExit();
                the_Process.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

public string unRAR(string rarPath, string rarName, string unRarPath)
        {
            String the_rar;
            RegistryKey the_Reg;
            Object the_Obj;
            String the_Info;
            ProcessStartInfo the_StartInfo;
            Process the_Process;
            try
            {
                the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications\\WinRAR.exe\\Shell\\Open\\Command");
                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 + "\\" + rarName.Substring(0, rarName.LastIndexOf(".")-1)))
                    Directory.CreateDirectory(unRarPath + "\\" + rarName.Substring(0, rarName.LastIndexOf(".")-1));
                the_Info = string.Format("x {0} {1}\\{2} -y", rarName, unRarPath, rarName.Substring(0, rarName.LastIndexOf(".")-1));
                the_StartInfo = new ProcessStartInfo();
                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();
                the_Process.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return "";//Server.MapPath(unRarPatch);
        }
        public string unRAR(string rarPath, string rarName, string unRarPath, string unRarPathName)
        {
            String the_rar;
            RegistryKey the_Reg;
            Object the_Obj;
            String the_Info;
            ProcessStartInfo the_StartInfo;
            Process the_Process;
            try
            {
                the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications\\WinRAR.exe\\Shell\\Open\\Command");
                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 + "\\" + unRarPathName))
                //    Directory.CreateDirectory(unRarPath + "\\" + unRarPathName);
                //the_Info = string.Format("x {0} {1}\\{2} -y", rarName, unRarPath, unRarPathName);

if (!Directory.Exists(unRarPath ))
                    Directory.CreateDirectory(unRarPath);
                the_Info = string.Format("x {0} {1} -y", rarName, unRarPath);
                the_StartInfo = new ProcessStartInfo();
                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();
                the_Process.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return "";//Server.MapPath(unRarPatch);
        }

RARsave("c:\\emrbs", "1.txt 2.txt", "c:\\test", "test.rar");

unRAR("d:\\","f.rar","d:\\ftest");

/压缩程序安装路径

string pathExe = Application.StartupPath + @"\WinRAR.exe";

Process p = new Process();

p.StartInfo.FileName = pathExe;

p.StartInfo.Arguments = @"a -as -r -afzip -ed -hp123 -ibck -inul -m5 -mt5 d:\kldder d:\easyui";

p.StartInfo.CreateNoWindow = true;

p.StartInfo.RedirectStandardInput = true;

p.StartInfo.RedirectStandardError = false;

p.StartInfo.RedirectStandardOutput = true;

p.StartInfo.UseShellExecute = false;

p.StartInfo.ErrorDialog = false;

p.Start();

int idx = 1;

while (!p.HasExited)

{

idx++;

p.WaitForExit(500);

if (idx == 5)

{

p.Kill();

}

}

p.Close();

p.Dispose();

/*

* <命令> -<开关1> -<开关N> <压缩文件 > <文件...> <@列表文件...> <解压路径\>

*压缩 a      a -as -ed -inul -afrar -df -ibck -m4 -mt3 -or -y -hp123 d:\aa d:\aa.txt

*解压 x      x -hp123 -ibck -inul -y -mt5 d:\aa.rar a:\

*a d:\Info.zip D:\easyui

*-af 指定格式 -afzip -afrar

*-as 在当前添加的文件列表中不存在的被压缩文件,将会从压缩文件中删除

*-df 压缩后删除源文件

*-dr 删除到回收站

*-ed 不添加空文件夹

*-hp 添加密码 -hp123456

*-ibck 后台运行

*-inul 禁止错误信息

*-loff 压缩完成后 关闭电源

*-m0 存储 添加文件到压缩文件但是不压缩

*-m1 最快 最快速的方法 ( 最低的压缩比)

*-m2 快速 快速压缩方法

*-m3 标准 标准 (默认 ) 压缩方法

*-m4 较好 较好的压缩方法 (较高的压缩比)

*-m5 最优 最优的压缩方法 (最高压缩比但是速度也最慢)

*-mtN 线程 -mt5 1~32

*-or 自动重命名文件

*-r 连同子文件

*-z 压缩后测试文件

*-y 所有弹窗选择"是"

*/

*可以直接将winrar.exe 拷贝到 根目录下运行

*不弹窗只需要打开开光 -ibck -inul 即可

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:/temp目录rar e temp.rar c:/temprar e *.rar c:/temp(支持批量操作) 
2、将temp.rar解压到c:/temp目录,并且解压后的目录结构和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

时间: 2024-08-03 11:32:32

C#调用RAR压缩与解压的相关文章

java压缩文件解压:调用WinRAR5命令强于自己写代码实现

最近,手上维护着一个几年前的系统,技术是用的JSP+Strust2,系统提供了rar和zip两种压缩格式的解压功能,后台是用java实现的 1.解压rar格式,采用的是java-unrar-0.3.jar 2.解压zip格式,采用的是commons-compress-1.4.1.jar 但最近根据用户反馈的问题,发现系统存在两个关于压缩文件解压的问题: 1.有些压缩文件解压之后出现中文乱码: 2.有些压缩文件根本不能解压 为了弥补上述两个问题,在之前代码的基础上打了一些补丁,来解决zip压缩包乱

WinRAR(WinZip)压缩与解压实现(C#版Window平台)

本文的原理是借助Windows平台安装的WinRAR(WinZip)实现C#程序的调用(注:WinRAR压缩解压WinZip同样适用). 先来看WinRAR(WinZip)自身的支持调用命令: 压缩命令:a {0} {1} -r [{0}:压缩后文件名|{1}:待压缩的文件物理路径] ex:"a 你妹.rar f:\\MM -r" (含义为将f盘下MM的文件夹压缩为"你妹.rar"文件) 解压命令:x {0} {1} -y [{0}:待解压文件名称|{1}:待解压文

Linux下常用压缩格式的压缩与解压方法

.tar 解包: tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) --------------------------------------------- .gz 解压1:gunzip FileName.gz 解压2:gzip -d FileName.gz 压缩:gzip FileName .tar.gz 解压:tar zxvf FileName.tar.gz 压缩:tar zcvf FileName.

linux下tar gz bz2 tgz z等众多压缩文件的压缩与解压方法

Linux下最常用的打包程序就是tar了,使用tar程序打出来的包我们常称为tar包,tar包文件的命令通常都是以.tar结尾的.生成tar包后,就可以用其它的程序来进 行压缩了,所以首先就来讲讲tar命令的基本用法: tar命令的选项有很多(用man tar可以查看到),但常用的就那么几个选项,下面 来举例说明一下: # tar -cf all.tar *.jpg 这条命令是将所有.jpg的文件打成一个名为all.tar的包.-c是表示产生新的包 ,-f指定包的文件名. # tar -rf a

linux下文件压缩与解压操作

对于刚刚接触Linux的人来说,一定会给Linux下一大堆各式各样的文件名给搞晕.别个不说,单单就压缩文件为例,我们知道在Windows下最常见的压缩文件就只有两种,一是,zip,另一个是.rap.可是Linux就不同了,它有.gz..tar.gz.tgz.bz2..Z..tar等众多的压缩文件名,此外windows下的.zip和.rar也可以在Linux下使用,不过在Linux使用.zip和.rar的人就太少了.本文就来对这些常见的压缩文件进行一番小结,希望你下次遇到这些文件时不至于被搞晕.

linux 压缩、解压 {大全}

.tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) --------------- .gz 解压1:gunzip FileName.gz 解压2:gzip -d FileName.gz 压缩:gzip FileName .tar.gz 和 .tgz 解压:tar zxvf FileName.tar.gz 压缩:tar zcvf FileName.tar.gz DirName ---------

linux 打包、压缩、解压

linux下打包.压缩.解压方法: 方法一: ==打包 # tar cvf 123.tar 目录名   将目录打包为123.tar的文件  打包后并不压缩 c--创建  v--详细  f--文件  x--解压  z---对应***.gz ==压缩 # gzip etc1.tar # bzip2 etc2.tar # xz etc3.tar ==解压 # gzip -d etc1.tar.gz # bzip2 -d etc2.tar.bz2 # xz -d etc3.tar.xz ==解包 # t

linux压缩、解压和归档

1      简介 压缩格式  gz bz2 xz zip Z 压缩算法:算法不同,压缩比也不相同 压缩比:(压缩前的文件大小-压缩后的文件大小)/压缩前的文件大小 文本文件压缩比大,图片视频比较小 xz>bz2>gz2>Z #这个对于大文件生效,小文件未必 常用的压缩解压工具: compress/uncompress .Z (比较老的压缩算法,比较少使用了) gzip/gunzip .gz (不支持目录压缩) bzip2/bunzip2 .bz2不支持目录压缩) xz/unxz .xz

Python调用7zip命令实现解压

Python调用7zip命令实现文件批量解压 1.输入压缩文件所在的路径 2.可以在代码中修改解压到的文件路径和所需要解压的类型,列入,解压文件夹下面所有的mp4格式的文件 3.cmd 指的就是Python调用dos命令 ,紧跟着的x -r这些全是参考7zip使用书册,及抽取文件夹下所有的以mp4结尾的文件,把文件放入e盘,测试解压文件夹下的new文件夹 # 调用7z命令批量解压文件 import os folder_name = input("请输入文件夹:") os.chdir(f