技术频道导航
HTML/CSS
.NET技术
IIS技术
PHP技术
Js/JQuery
Photoshop
Fireworks
服务器技术
操作系统
网站运营
卡卡网是专业的网站测速平台,网速测试,测试网站速度,就来卡卡网 ~
问题反馈网络日志

【DiyVM】沙田机房/香港云/回国CN2线路/AMD EPYC/39元一月5M/CN2海外云主机 24元/月BGP+CN2海外云 低至25元/月海外主机 低至$2/月

DiyVM:香港VPS惊爆价36元一月
★站长变现★特色悬浮小图标广告
5M CN2 GIA云主机 24元起
【转化好产品,官方高价收量】
一一一云主机 26元起一一一
官方高价收量,每日稳定结算

一一云主机 24元 3折起一一
AWS核心代理U充值 免注册开户
海外CN2云 低至$2.5/月
海外云低至2折 298/年
免费测试★APK免杀 谷歌过保护
官方收量CPA/CPS长期稳定

海外主机 5M CN2 低至$2/月
恒创科技 一 海外服务器 ● 高速稳定
★解决安装报毒★谷歌过保护机制
CN2 GIA/1000Mbps $111/月
超级签★免杀★加固★满意付款
全球云主机 3天试用再买

【菠萝云】香港4G内存99元,马上开通
亿人互联-津/京BGP托管租用/VPS
苹果签名/APP封装/远控免杀
10M CN2海外云VPS 53元/月
一一站长/主播好变现一一有流量就来
站长变现 特色悬浮小图标广告

实力产品变现
实力产品变现
实力产品变现
实力产品变现
实力产品变现
实力产品变现

赞助商

分类目录

赞助商

最新文章

Windows 2012 不默认安装 .NET 3.5,需要此环境的人要手动添加...
在本文中,我将介绍如何在 .NET 中美化 JSON 字符串。
asp.net 的 @OutputCache 是以声明方式控制 ASP.NET ...
在本教程中,您将了解 System.IO,它是一个 C# 命名空间。此命名空间提...
在本文中,我将介绍C#如何清理JSON字符串里的HTML标签,同时把双引号变为单...
== 运算符和 Equals() 方法都用于比较两个值类型数据项或引用类型数据项...
为了比较变量之间的相等性,C# 提供了两种比较方法“==&rdquo...

搜索

ASP.NET压缩及解压文件,无需第三方DLL插件

作者:admin    时间:2022-9-6 10:59:56    浏览:247

在前面曾介绍过ASP.NET用DotNetZip压缩文件,它的优点是简单易用,代码很少,不过需要额外引用一个DLL插件。在本文中,我将介绍一个不用任何第三方插件的方法,来实现压缩及解压文件。

实现原理

使用本机安装的RAR压缩软件的接口,调用进程的方法,实现文件的压缩和解压缩。

下面是实现代码。

压缩文件

首先需要引用两个命名空间:

  1. using Microsoft.Win32;
  2. using System.Diagnostics;

压缩文件函数:

  1. ///
  2. /// 压缩文件
  3. ///
  4. /// 需要压缩的文件夹或者单个文件
  5. /// 生成压缩文件的文件名
  6. /// 生成压缩文件保存路径
  7. ///
  8. protected bool RAR(string DFilePath, string DRARName, string DRARPath)
  9. {
  10. String therar;
  11. RegistryKey theReg;
  12. Object theObj;
  13. String theInfo;
  14. ProcessStartInfo theStartInfo;
  15. Process theProcess;
  16. try
  17. {
  18.  
  19. theReg = Registry.ClassesRoot.OpenSubKey(@"WinRAR\Shell\Open\Command");
  20. theObj = theReg.GetValue("");
  21. therar = theObj.ToString();
  22. theReg.Close();
  23. therar = therar.Substring(1, therar.Length - 7);
  24. theInfo = " a    " + " " + DRARName + "  " + DFilePath + " -ep1"; //命令 + 压缩后文件名 + 被压缩的文件或者路径
  25. theStartInfo = new ProcessStartInfo();
  26. theStartInfo.FileName = therar;
  27. theStartInfo.Arguments = theInfo;
  28. theStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  29. theStartInfo.WorkingDirectory = DRARPath; //RaR文件的存放目录。
  30. theProcess = new Process();
  31. theProcess.StartInfo = theStartInfo;
  32. theProcess.Start();
  33. theProcess.WaitForExit();
  34. theProcess.Close();
  35. return true;
  36. }
  37. catch (Exception ex)
  38. {
  39. return false;
  40. }
  41. }

代码解释

WinRAR\Shell\Open\Command 这个是RAR在注册表的命令接口位置,它在 ClassesRoot 节点里。你安装RAR软件后,可以在注册表里查看一下该位置是否正确。

theProcess = new Process(); 是调用进程来执行方法。

函数调用:

  1. RAR(@"F:\dotnet20Test\Cache", "Cache", @"F:\dotnet20Test\");

F:\dotnet20Test\Cache 是压缩文件夹;Cache 是压缩生成的文件名; F:\dotnet20Test\ 是压缩文件存放位置。

该函数返回一个bool值,truefalse,我们可以用if语句来判断是否压缩成功。

  1. if (RAR(@"F:\dotnet20Test\Cache", "Cache", @"F:\dotnet20Test\"))
  2. {
  3. Response.Write("ok");
  4. }
x

解压文件

上面说了压缩,那么就有解压,下面是相关代码。

解压文件函数:

  1. ///
  2. /// 解压缩到指定文件夹
  3. ///
  4. /// 压缩文件存在的目录
  5. /// 压缩文件名称
  6. /// 解压到文件夹
  7. ///
  8. protected bool UnRAR(string RARFilePath, string RARFileName, string UnRARFilePath)
  9. {
  10. //解压缩
  11. String therar;
  12. RegistryKey theReg;
  13. Object theObj;
  14. String theInfo;
  15. ProcessStartInfo theStartInfo;
  16. Process theProcess;
  17. try
  18. {
  19. theReg = Registry.ClassesRoot.OpenSubKey(@"WinRAR\Shell\Open\Command");
  20. theObj = theReg.GetValue("");
  21. therar = theObj.ToString();
  22. theReg.Close();
  23. therar = therar.Substring(1, therar.Length - 7);
  24. theInfo = @" X " + " " + RARFilePath + RARFileName + " " + UnRARFilePath;
  25. theStartInfo = new ProcessStartInfo();
  26. theStartInfo.FileName = therar;
  27. theStartInfo.Arguments = theInfo;
  28. theStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  29. theProcess = new Process();
  30. theProcess.StartInfo = theStartInfo;
  31. theProcess.Start();
  32. return true;
  33. }
  34. catch (Exception ex)
  35. {
  36. return false;
  37. }
  38. }

函数调用:

  1. UnRAR(@"F:\dotnet20Test\", "Cache.rar", @"F:\dotnet20Test\unrar\");

F:\dotnet20Test\ 是解压文件的位置路径;Cache.rar 是解压文件; F:\dotnet20Test\unrar\ 是解压文件要存放的位置。

该函数返回一个bool值,truefalse,我们可以用if语句来判断是否解压成功。

  1. if (UnRAR(@"F:\dotnet20Test\", "Cache.rar", @"F:\dotnet20Test\unrar\"))
  2. {
  3.     Response.Write("ok");
  4. }

总结

本文介绍了ASP.NET压缩及解压文件的方法,无需第三方DLL插件,通过调用RAR软件的接口,使用进程方法来实现。

文件下载

downloadtxt.aspx.cs

相关文章

标签: 压缩文件  
x
广告: 【限时】云主机 24元/月