您的位置:首页 > 编程语言 > C#

利用C#调用WINRAR实现压缩与解压

2013-03-13 13:14 393 查看
路径名长度小于 259 个字符。

最大的压缩文件注释的长度是62000字节。

命令限制:

命令 'd','u','f','c','cf' 不能用于分卷压缩文件。

命令 'a' 不能用来更新分卷压缩文件,只能用来创建。

返回值

~~~~~~

RAR 成功操作后返回 0 。非 0 返回码意味着操作由于某种错误被取消:

255 用户中断 用户中断操作

9 创建错误 创建文件错误

8 内存错误 没有足够的内存进行操作

7 用户错误 命令行选项错误

6 打开错误 打开文件错误

5 写错误 写入磁盘错误

4 被锁定压缩文件 试图修改先前使用 'k' 命令锁定的压缩文件

3 CRC 错误 解压缩时发生一个 CRC 错误

2 致命错误 发生一个致命错误

1 警告 没有发生致命错误

0 成功 操作成功

protected void Button1_Click(object sender, EventArgs e)

{

string strtxtPath = "C:\\freezip\\free.txt";

string strzipPath = "C:\\freezip\\free.zip";

System.Diagnostics.Process Process1 = new System.Diagnostics.Process();

Process1.StartInfo.FileName = "Winrar.exe";

Process1.StartInfo.CreateNoWindow = true;

Process1.StartInfo.Arguments = " a -r " + strzipPath + " " + strtxtPath;

Process1.Start();

Process1.WaitForExit();

if (Process1.HasExited)

{

int iExitCode = Process1.ExitCode;

if (iExitCode == 0)

{

//正常完成

}

else

{

//有错

}

}

Process1.Close();

}

public void RARsave(string patch,string rarPatch,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(@"ApplicationsWinRAR.exeShellOpenCommand");

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 = 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 unRarPatch,string rarPatch,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(@"ApplicationsWinRAR.exeShellOpenCommand");

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(Server.MapPath(unRarPatch));

the_Info = "e " + rarName + " " + Server.MapPath(unRarPatch) + " -y";

the_StartInfo = new ProcessStartInfo();

the_StartInfo.FileName = the_rar;

the_StartInfo.Arguments = the_Info;

the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

the_StartInfo.WorkingDirectory = Server.MapPath(rarPatch);//获取压缩包路径

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);

}

更多WinRAR命令(引自WinRAR帮助文档)

a 添加文件到压缩文件

c 添加压缩文件注释

d 从压缩文件删除文件

e 从压缩文件解压压缩,忽略路径

f 刷新压缩文件中的文件

i 在压缩文件中查找字符串

k 锁定压缩文件

m 移动文件和文件夹到压缩文件

r 修复受损的压缩文件

rc 重建丢失的卷

rn 重命名压缩文件

rr
添加数据恢复记录

rv
创建恢复卷

s[name] 转换压缩文件成为自解压文件类型

s- 删除自解压模块

t 测试压缩文件

u 从压缩文件中更新文件

x 以完整路径名称从压缩文件解压压缩

更多WinRAR字母开头(引自WinRAR帮助文档)

-ac 在压缩或解压后清除存档属性

-ad 附加压缩文件名到目标路径中

-af<类型> 指定压缩文件格式

-ag[格式] 以当前日期生成压缩文件名

-ao 添加有存档属性设置的

-ap 设置内部压缩文件路径

-as 同步化压缩文件内容

-av 应用用户身份校验信息

-av- 禁用添加用户身份校验信息

-cfg- 忽略默认配置和环境变量

-cl 将文件名转换成为小写

-cu 将文件名转换成为大写

-df 压缩后删除压缩文件

-dh 打开共享的文件

-ds 不排序压缩的文件

-ed 不添加空文件夹

-en 不添加“压缩文件结束”块

-ep 从名称中排除路径

-ep1 从名称中排除主文件夹

-ep2 扩大成完整路径

-ep3 扩展包含盘符的完整路径

-e[+]<属性> 设置文件排除和包含属性

-f 刷新文件

-hp[密码] 加密文件数据和头

-ibck 在后台运行 WinRAR

-ieml 使用E-mail发送压缩文件

-iicon<名称> 指定自解压图标

-iimg<名称> 指定自解压图片

-ilog[名称] 记录错误到文件中

-inul 关闭错误信息

-ioff 关闭 PC 电源

-k 锁定压缩文件

-kb 保留坏掉的解压文件

-m<n> 设置压缩方式

-mc<参数> 设置高级压缩参数

-md<n> 选择字典大小

-ms 指定文件存储

-n<文件> 仅包含指定的文件

-n@<列表文件> 使用指定的列表文件包含文件

-os 保存 NTFS 数据流

-oc 设置 NTFS “压缩”属性

-ow 处理文件用户身份校验信息

-o+ 覆盖已存在的文件

-o- 不要覆盖已存在的文件

-p[密码] 设置密码

-r 返回子文件夹

-r0 只返回与通配符匹配的子文件夹

-ri 设置优先级和休眠时间

-rr
添加数据恢复记录

-rv
创建恢复卷

-s 创建固实压缩文件

-s<N> 以文件数量创建固实组

-se 以文件扩展名创建固实组

-sfx[名称] 创建自解压文件

-sv 创建互不依赖的固实压缩文件

-sv- 创建互相依赖的固实压缩文件

-s- 禁用固实算法

-t 压缩后测试文件

-ta<日期> 只处理指定日期之后修改的文件

-tb<日期> 只处理指定日期之前修改的文件

-tk 保持原有压缩文件时间

-tl 以最新的文件设置压缩文件时间

-tn<时间> 处理较新于指定时间的文件

-to<时间> 处理较旧于指定时间的文件

-ts<m,c,a> 保存或恢复文件时间(修改,创建,访问)

-u 更新文件

-v<n>[k|b|f|m|M|g|G] 创建分卷压缩

-vd 创建分卷压缩前清除磁盘内容

-ver 文件版本控制

-vn 使用旧风格的卷命名法则

-vp 每次分卷压缩前暂停

-x<文件> 排除指定的文件

-x@<文件列表> 使用指定的列表文件来排除指定的文件

-y 假设全部的询问回应皆为“是”

-z<文件> 从文件读取压缩文件注释

-- 停止开关的扫描

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: