【C#】关于使用WinRAR进行文件的RAR、ZIP格式解压和压缩操作
|
admin
2025年2月13日 17:11
本文热度 1162
|
1. 利用 WinRAR 进行解压缩
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
using Microsoft.Win32;
namespace RarClass
{
public class unrar
{
public void unCompressRAR(string unRarPatch, string rarPatch, string rarName)
{
string the_rar;
RegistryKey the_Reg;
object the_Obj;
string the_Info;
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();
if (Directory.Exists(unRarPatch) == false)
{
Directory.CreateDirectory(unRarPatch);
}
the_Info = "x -y " + rarName + " " + unRarPatch ;
ProcessStartInfo the_StartInfo = new ProcessStartInfo();
the_StartInfo.FileName = the_rar;
the_StartInfo.Arguments = the_Info;
the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
the_StartInfo.WorkingDirectory = rarPatch;
Process the_Process = new Process();
the_Process.StartInfo = the_StartInfo;
the_Process.Start();
the_Process.WaitForExit();
the_Process.Close();
}
}
}
2. 利用 WinRAR 进行解压缩
public bool RAR(string path, string rarPath, string rarName)
{
bool flag = false;
string rarexe;
RegistryKey regkey;
Object regvalue;
string cmd;
ProcessStartInfo startinfo;
Process process;
try
{
regkey = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\shell\open\command");
regvalue = regkey.GetValue("");
rarexe = regvalue.ToString();
regkey.Close();
rarexe = rarexe.Substring(1, rarexe.Length - 7);
Directory.CreateDirectory(path);
cmd = string.Format("a {0} {1} -r", rarName, path);
startinfo = new ProcessStartInfo();
startinfo.FileName = rarexe;
startinfo.Arguments = cmd;
startinfo.WindowStyle = ProcessWindowStyle.Hidden;
startinfo.WorkingDirectory = rarPath;
process = new Process();
process.StartInfo = startinfo;
process.Start();
process.WaitForExit();
if (process.HasExited)
{
flag = true;
}
process.Close();
}
catch (Exception e)
{
throw e;
}
return flag;
}
public bool UnRAR(string path, string rarPath, string rarName)
{
bool flag = false;
string rarexe;
RegistryKey regkey;
Object regvalue;
string cmd;
ProcessStartInfo startinfo;
Process process;
try
{
regkey = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\shell\open\command");
regvalue = regkey.GetValue("");
rarexe = regvalue.ToString();
regkey.Close();
rarexe = rarexe.Substring(1, rarexe.Length - 7);
cmd = string.Format("x {0} {1} -y",
rarName,
path);
startinfo = new ProcessStartInfo();
startinfo.FileName = rarexe;
startinfo.Arguments = cmd;
startinfo.WindowStyle = ProcessWindowStyle.Hidden;= rarPath;
process = new Process();
process.StartInfo = startinfo;
process.Start();
process.WaitForExit();
if (process.HasExited)
{
flag = true;
}
process.Close();
}
catch (Exception e)
{
throw e;
}
return flag;
}
Directory.CreateDirectory(path);
startinfo.WorkingDirectory
昨天又看了下,发现如果路径中有空格(如:D:\Program Files\)的话压缩解压就会出现问题,折磨了我很长时间,最后实在没办法了就在cmd里面试了半天,发现在有空格的路径上加双引号就可以了。在代码里Directory.CreateDirectory(path);后面把 path 和 rarName 都判断一下如果有空格,就加上 path = "\"" + path + "\"";
3. 本次示例主要实现:
1.压缩文件夹及其下文件
2.压缩文件夹下文件
3.压缩文件夹及其下文件为rar 还是 zip
4.解压缩
5.加密压缩及解加密压缩
———–
示例代码如下:
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.Start();
if (Process1.HasExited)
{
int iExitCode = Process1.ExitCode;
if (iExitCode == 0)
{
Response.Write(iExitCode.ToString() + " 正常完成");
}
else
{
Response.Write(iExitCode.ToString() + " 有错完成");
}
}
}
4. C#调用rar.exe解压一个rar文件到系统的临时目录
string sysTempDir = Path.GetTempPath();
string rarFilePath = @"d:\test.rar";
string unrarDestPath = Path.Combine(sysTempDir, Path.GetFileNameWithoutExtension(rarFilePath));
string shellArguments = string.Format("x -o+ \"{0}\" \"{1}\\\"", rarFilePath, unrarDestPath);
using (Process unrar = new Process())
{
unrar.StartInfo.FileName = "rar.exe";
unrar.StartInfo.Arguments = shellArguments;
unrar.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
unrar.Start();
unrar.WaitForExit();
unrar.Close();
}
DirectoryInfo di = new DirectoryInfo(unrarDestPath);
MessageBox.Show(string.Format("解压完成,共解压出:{0}个目录,{1}个文件", di.GetDirectories().Length, di.GetFiles().Length));
该文章在 2025/2/13 17:12:42 编辑过