最近在项目中需要读取winrar文件,为解决此问题,查找了一下百度,发现很多的解决方案是直接调用winrar程序,解压缩后再读取文件,关于这个方案的代码如下:
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程序对于解压缩提供了一个免费的工具,以及部分源代码,部分的 UNRAR 原始码可在下列网址获得 http://www.rarlab.com,
后来在http://www.rarlab.com/rar_add.htm 网页上找到了该源码,编译后,按照自己需要的接口,包装一层,问题解决 ,而且还可以查看rar文件的内容,解压进度显示等都可以实现。