admin管理员组文章数量:1632318
其实这个东西网上已经有很多了 给出了一大把 当然我也是在网上找到得 只不过 说明不够详细
经过测试 给出详细的备注; 解压的给的很详细 压缩的基本也一样 只不过参数信息不一样罢了;
利用winrar.exe的方法 其实也是因为没有办法 因为这中方法支持的格式最多
当然除了这种方法外 最起码还有三种方法可以操作(
1: ICSharpCode.SharpZipLib(开源的) 只支持zip格式的;
2: SharpCompress (用开源的就行) 只是这种方式 实际测试中.z 后缀的压缩包不能解压 当然还有一些其他的问题(如果哪位大神 要研究可以 去修改源码看看)
3: SevenZipSharp 这种我没有试 但看介绍 好像 格式也有一些不支持;
下面就给出咱们最笨的最有效的方法
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.IO;
using System.Diagnostics;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public string ToPath = @"D:\RARTest";
//用于启动winrar的进程
Process rarProcess;
public Form1()
{
InitializeComponent();
}
//保存文件路径信息 (包含文件名和后缀名)
string filePath=string.Empty;
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string file = openFileDialog1.FileName;
this.textBox1.Text = file;
filePath = file;
}
}
//开始解压操作
private void button2_Click(object sender, EventArgs e)
{
try
{
//判断是不是有 winrar 软件 有的话就执行 没有那就哪里凉快去哪里吧
if (Exists())
{
//得到文件所在的目录
string rardir = Path.GetDirectoryName(filePath);
//得到文件名 包含后缀
string rarname = Path.GetFileName(filePath);
//进行解压操作
UnCompressRAR(ToPath, rardir, rarname);
}
else
{
MessageBox.Show("没有安装 winrar");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public bool Exists()
{
//通过注册表信息 判断是不是装有 winrar 软件
RegistryKey the_Reg =
Registry.LocalMachine.OpenSubKey(
@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
return !string.IsNullOrEmpty(the_Reg.GetValue("").ToString());
}
//解压
///
/// 解压操作 当然 winrar 不单单只是说支持rar的文件的操作 可以有很多种格式 几乎所有
///
/// 要解压到得路径
/// rar文件目录路径
/// rar文件名 包括后缀
///
public string UnCompressRAR(string unRarPatch, string rarPatch, string rarName)
{
string the_rar;
RegistryKey the_Reg;
object the_Obj;
string the_Info;
try
{
//拿到我们winrar注册信息
the_Reg =
Registry.LocalMachine.OpenSubKey(
@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
//具体都有哪些键值 没有参考过 但是测试得到这个 得到的就是 winrar.exe的真是路径 用object接受
the_Obj = the_Reg.GetValue("");
//只不过是把 winrar.exe 路径转换成了string的形式 其实 这里有很多代码是可以简化的 但是为了直观 不做过多的处理
the_rar = the_Obj.ToString();
//关闭对注册表项的更改
the_Reg.Close();
//如果不存在 我们准备解压到得路径 那么就创建这个路径
if (Directory.Exists(unRarPatch) == false)
{
Directory.CreateDirectory(unRarPatch);
}
//winrar的 命令参数 这个是解压到指定目录 覆盖文件
the_Info = "x " + rarName + " " + unRarPatch + " -y";
//指定启动进程时要给的一些参数
ProcessStartInfo the_StartInfo = new ProcessStartInfo();
//winrar.exe路径信息
the_StartInfo.FileName = the_rar;
//执行的命令行参数
the_StartInfo.Arguments = the_Info;
//窗体弹窗设置为隐藏
the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
the_StartInfo.WorkingDirectory = rarPatch;//获取压缩包路径
rarProcess = new Process();
rarProcess.StartInfo = the_StartInfo;
rarProcess.Start();
rarProcess.WaitForExit();
if (rarProcess.HasExited)
{
int exitCode = rarProcess.ExitCode;
if (exitCode != 0)
{
//8 内存错误 没有足够的内存进行操作
//7 用户错误 命令行选项错误
//6 打开错误 打开文件错误
//5 写错误 写入磁盘错误
//4 被锁定压缩文件 试图修改先前使用 'k' 命令锁定的压缩文件
//3 CRC 错误 解压缩时发生一个 CRC 错误
//2 致命错误 发生一个致命错误
//1 警告 没有发生致命错误
//0 成功 操作成功
switch (exitCode)
{
case 1:
throw new Exception("警告 没有发生致命错误");
break;
case 2:
throw new Exception("致命错误 发生一个致命错误");
break;
case 3:
throw new Exception("CRC 错误 解压缩时发生一个 CRC 错误");
break;
case 4:
throw new Exception("被锁定压缩文件 试图修改先前使用 'k' 命令锁定的压缩文件");
break;
case 5:
throw new Exception("写错误 写入磁盘错误");
break;
case 6:
throw new Exception("打开错误 打开文件错误");
break;
case 7:
throw new Exception("用户错误 命令行选项错误");
break;
case 8:
throw new Exception("内存错误 没有足够的内存进行操作");
break;
}
throw new Exception("致命错误 发生一个致命错误: "+exitCode+" ");
}
}
rarProcess.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (rarProcess != null)
{
rarProcess.Close();
}
}
return unRarPatch;
}
//压缩
public void CompressRAR(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.LocalMachine.OpenSubKey(
@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
the_Obj = the_Reg.GetValue("");
the_rar = the_Obj.ToString();
the_Reg.Close();
Directory.CreateDirectory(patch);
//命令参数
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;
}
}
}
}
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
版权声明:本文标题:c# rar解压大小_C#解压缩Zip,Rar等压缩文件(详细说明) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1729113684a1187061.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论