在前面曾介绍过ASP.NET用DotNetZip压缩文件,它的优点是简单易用,代码很少,不过需要额外引用一个DLL插件。在本文中,我将介绍一个不用任何第三方插件的方法,来实现压缩及解压文件。
实现原理
使用本机安装的RAR压缩软件的接口,调用进程的方法,实现文件的压缩和解压缩。
下面是实现代码。
压缩文件
首先需要引用两个命名空间:
- using Microsoft.Win32;
- using System.Diagnostics;
压缩文件函数:
- ///
- /// 压缩文件
- ///
- /// 需要压缩的文件夹或者单个文件
- /// 生成压缩文件的文件名
- /// 生成压缩文件保存路径
- ///
- protected bool RAR(string DFilePath, string DRARName, string DRARPath)
- {
- String therar;
- RegistryKey theReg;
- Object theObj;
- String theInfo;
- ProcessStartInfo theStartInfo;
- Process theProcess;
- try
- {
-
- theReg = Registry.ClassesRoot.OpenSubKey(@"WinRAR\Shell\Open\Command");
- theObj = theReg.GetValue("");
- therar = theObj.ToString();
- theReg.Close();
- therar = therar.Substring(1, therar.Length - 7);
- theInfo = " a " + " " + DRARName + " " + DFilePath + " -ep1"; //命令 + 压缩后文件名 + 被压缩的文件或者路径
- theStartInfo = new ProcessStartInfo();
- theStartInfo.FileName = therar;
- theStartInfo.Arguments = theInfo;
- theStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
- theStartInfo.WorkingDirectory = DRARPath; //RaR文件的存放目录。
- theProcess = new Process();
- theProcess.StartInfo = theStartInfo;
- theProcess.Start();
- theProcess.WaitForExit();
- theProcess.Close();
- return true;
- }
- catch (Exception ex)
- {
- return false;
- }
- }
新窗显示代码
复制代码
代码解释
WinRAR\Shell\Open\Command
这个是RAR在注册表的命令接口位置,它在 ClassesRoot
节点里。你安装RAR软件后,可以在注册表里查看一下该位置是否正确。
theProcess = new Process();
是调用进程来执行方法。
函数调用:
- RAR(@"F:\dotnet20Test\Cache", "Cache", @"F:\dotnet20Test\");
F:\dotnet20Test\Cache
是压缩文件夹;Cache
是压缩生成的文件名; F:\dotnet20Test\
是压缩文件存放位置。
该函数返回一个bool
值,true
或false
,我们可以用if
语句来判断是否压缩成功。
- if (RAR(@"F:\dotnet20Test\Cache", "Cache", @"F:\dotnet20Test\"))
- {
- Response.Write("ok");
- }
新窗显示代码
复制代码
x
解压文件
上面说了压缩,那么就有解压,下面是相关代码。
解压文件函数:
- ///
- /// 解压缩到指定文件夹
- ///
- /// 压缩文件存在的目录
- /// 压缩文件名称
- /// 解压到文件夹
- ///
- protected bool UnRAR(string RARFilePath, string RARFileName, string UnRARFilePath)
- {
- //解压缩
- String therar;
- RegistryKey theReg;
- Object theObj;
- String theInfo;
- ProcessStartInfo theStartInfo;
- Process theProcess;
- try
- {
- theReg = Registry.ClassesRoot.OpenSubKey(@"WinRAR\Shell\Open\Command");
- theObj = theReg.GetValue("");
- therar = theObj.ToString();
- theReg.Close();
- therar = therar.Substring(1, therar.Length - 7);
- theInfo = @" X " + " " + RARFilePath + RARFileName + " " + UnRARFilePath;
- theStartInfo = new ProcessStartInfo();
- theStartInfo.FileName = therar;
- theStartInfo.Arguments = theInfo;
- theStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
- theProcess = new Process();
- theProcess.StartInfo = theStartInfo;
- theProcess.Start();
- return true;
- }
- catch (Exception ex)
- {
- return false;
- }
- }
新窗显示代码
复制代码
函数调用:
- UnRAR(@"F:\dotnet20Test\", "Cache.rar", @"F:\dotnet20Test\unrar\");
F:\dotnet20Test\
是解压文件的位置路径;Cache.rar
是解压文件; F:\dotnet20Test\unrar\
是解压文件要存放的位置。
该函数返回一个bool
值,true
或false
,我们可以用if
语句来判断是否解压成功。
- if (UnRAR(@"F:\dotnet20Test\", "Cache.rar", @"F:\dotnet20Test\unrar\"))
- {
- Response.Write("ok");
- }
新窗显示代码
复制代码
总结
本文介绍了ASP.NET压缩及解压文件的方法,无需第三方DLL插件,通过调用RAR软件的接口,使用进程方法来实现。
文件下载
downloadtxt.aspx.cs
相关文章