Asp.net在线压缩和解压缩简单实现(附项目源码)
首先服務器上要確保安裝rar軟件,且對相應目錄有可寫權限,該代碼利用遞歸顯示文件目錄,實現文件在線壓縮和解壓并測試成功。
這里參照了一些網絡文檔(清清月兒),代碼有詳細注釋,便于大家學習和參考。rar和unRar分別為壓縮文件和解壓文件存放目錄。
以下是代碼片斷(注意命名空間和rar命令參數)
usingMicrosoft.Win32;
usingSystem.Runtime.InteropServices;
usingSystem.Diagnostics;
//該源碼下載自www.51aspx.com(51aspx.com)
namespaceTreeRar
{
????public?partial?class?_Default?:?System.Web.UI.Page
????{
????????protected?void?Page_Load(object?sender,?EventArgs?e)
????????{
????????????//遞歸展示文件目錄
????????????string[]?dir?=?Directory.GetDirectories(Server.MapPath("\\"));
????????????Response.Write("<table???width=100%???border=1???rules=rows???cellspacing=0???colspacing=0><tr???height=60???bgColor=#339900><td?colspan=3?style=\"color=#FFFFFF\">目錄列表:</td></tr>");
????????????for?(int?i?=?0;?i?<?dir.Length;?i++)
????????????{
????????????????string?href?=?dir[i];
????????????????href?=?"http://"+Request.Url.Authority.ToString()?+?href.Substring(href.LastIndexOf("\\"));
????????????????Response.Write("<tr?????οnmοuseοut=\"this.style.backgroundColor='#ffffff'\"???οnmοuseοver=\"this.style.backgroundColor='#cccccc'\"><td> </td><td>"?+?dir[i]?+?"</td><td><a???href="?+?href?+?">打開</a></td></tr>");
????????????}
????????????Response.Write("</table>");
????????????lbTree.Text?=?Server.MapPath("\\");
????????????//???在此處放置用戶代碼以初始化頁面???
????????}
????????//得到當前IP
????????private?string?GetIP()
????????{
????????????string[]?IP_Ary;
????????????string?strIP,?strIP_list;
????????????strIP_list?=?Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
????????????if?(strIP_list?!=?null?&&?strIP_list?!=?"")
????????????{
????????????????strIP_list?=?strIP_list.Replace("'",?"");
????????????????if?(strIP_list.IndexOf(",")?>=?0)
????????????????{
????????????????????IP_Ary?=?strIP_list.Split(',');
????????????????????strIP?=?IP_Ary[0];
????????????????}
????????????????else
????????????????{
????????????????????strIP?=?strIP_list;
????????????????}
????????????}
????????????else
????????????{
????????????????strIP?=?"";
????????????}
????????????if?(strIP?==?"")
????????????{
????????????????strIP?=?Request.ServerVariables["REMOTE_ADDR"];
????????????????strIP?=?strIP.Replace("'",?"");
????????????}
????????????return?strIP;
????????}
????????protected?void?btnZip_Click(object?sender,?EventArgs?e)
????????{
??????????//壓縮
????????String?my51aspxRar;
????????RegistryKey?my51aspxReg;
????????Object?my51aspxObj;
????????String?my51aspxInfo;
????????ProcessStartInfo?my51aspxStartInfo;
????????Process?my51aspxProcess;
????????try
????????{
????????????my51aspxReg?=?Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\Shell\Open\Command");
????????????my51aspxObj?=?my51aspxReg.GetValue("");
????????????my51aspxRar?=?my51aspxObj.ToString();
????????????my51aspxReg.Close();
????????????my51aspxRar?=?my51aspxRar.Substring(1,?my51aspxRar.Length?-?7);
????????????my51aspxInfo?=?"a??-r??-ep1?51aspx.rar?"?+?lbTree.Text?+?@"rar/51aspx.txt?"+?lbTree.Text?+?@"rar";//這里為rar的壓縮命令格式(也可以自行擴展)
????????????my51aspxStartInfo?=?new?ProcessStartInfo();
????????????my51aspxStartInfo.FileName?=?my51aspxRar;
????????????my51aspxStartInfo.Arguments?=?my51aspxInfo;
????????????my51aspxStartInfo.WindowStyle?=?ProcessWindowStyle.Hidden;
????????????my51aspxStartInfo.WorkingDirectory?=?lbTree.Text?+?"rar";?;//獲取或設置要啟動的進程的初始目錄。
????????????my51aspxProcess?=?new?Process();
????????????my51aspxProcess.StartInfo?=?my51aspxStartInfo;
????????????my51aspxProcess.Start();
????????????Response.Write("<font?color=red>壓縮成功</font>");
????????}
????????catch?(Exception?ex)
???????{
????????????Response.Write(ex.ToString());
????????}
????????}
????????protected?void?btnUnZip_Click(object?sender,?EventArgs?e)
????????{
?????????//解壓縮
????????String?my51aspxRar;
????????RegistryKey?my51aspxReg;
????????Object?my51aspxObj;
????????String?my51aspxInfo;
????????ProcessStartInfo?my51aspxStartInfo;
????????Process?my51aspxProcess;
????????try
????????{
????????????my51aspxReg?=?Registry.ClassesRoot.OpenSubKey(@"Applications\WinRar.exe\Shell\Open\Command");
????????????my51aspxObj?=?my51aspxReg.GetValue("");
????????????my51aspxRar?=?my51aspxObj.ToString();
????????????my51aspxReg.Close();
????????????my51aspxRar?=?my51aspxRar.Substring(1,?my51aspxRar.Length?-?7);
????????????my51aspxInfo?=?"?X?"?+?lbTree.Text?+?@"rar/51aspx.rar?"?+?"?"?+?lbTree.Text?+?@"unRar";
????????????my51aspxStartInfo?=?new?ProcessStartInfo();
????????????my51aspxStartInfo.FileName?=?my51aspxRar;
????????????my51aspxStartInfo.Arguments?=?my51aspxInfo;
????????????my51aspxStartInfo.WindowStyle?=?ProcessWindowStyle.Hidden;
????????????my51aspxProcess?=?new?Process();
????????????my51aspxProcess.StartInfo?=?my51aspxStartInfo;
????????????my51aspxProcess.Start();
????????????Response.Write("<font?color=red>解壓縮成功</font>");
????????}
????????catch?(Exception?ex)
????????{
????????????Response.Write(ex.ToString());
????????}
????????}
????}
}
以下是rar命令語法說明
?? 從命令行也可以運行 WinRAR 命令,常規的命令行語法描述如下:
? WinRAR? <命令> -<開關1> -<開關N> <壓縮文件> <文件...> <@列表文件...> <解壓路徑\>
命令?要 WinRAR 運行的字符組合代表功能
開關?切換操作指定類型,壓縮強度,壓縮文件類型,等等的定義。
壓縮文件?要處理的壓縮文件名。
文件?要處理的文件名。
列表文件?列表文件是包含要處理文件名稱的純文本。文件名應該在第一卷啟動。可以在列表文件中使用//字符后添加注釋。例如,你可以包含兩列字符串創建 backup.lst:c:\work\doc\*.txt //備份文本文檔???? c:\work\image\*.bmp //備份圖片c:\work\misc并接著運行: winrar a backup @backup.lst你可以在命令行中同時指定普通的文件名和列表文件名。
解壓路徑?只與命令 e 和 x ,搭配使用。指出解壓文件添加的位置。如果文件夾不存在時,會自動創建。
注意事項
a) 如果未指定 文件 或是 列表文件 時,WinRAR 將會以缺省的 *.* 運行全部的文件;
b) 如果未指定壓縮文件擴展名時,WinRAR 將會使用在 壓縮配置 中選定的默認壓縮文件格式。但你可以指定 .RAR 或 .ZIP 擴展名來替換它們;
c) 在命令行所輸入的開關會替換相同的配置設置值;
d) 在命令 c、e、s、t、rr、k 和 x 可在壓縮文件名中使用通配符。如此可以用單個的命令來進行超過一個以上的壓縮文件,除此之外,如果你指定 -r 開關于這些命令時,它們將會搜索在子文件夾中的壓縮文件;
e) 某些命令和開關只應用在 RAR 壓縮文件,有些則在 RAR 和 ZIP 都可使用,而某些則可應用在全部的壓縮文件格式。這一些都得看壓縮文件格式所提供的特性而定;
f) 命令和開關的大小寫是相同意思的,你可以用大寫或者小寫來下命令均可
本文轉自 liudao 博客園博客,原文鏈接:http://www.cnblogs.com/liudao/archive/2007/09/06/884095.html,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的Asp.net在线压缩和解压缩简单实现(附项目源码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IntelliJ IDEA删除所有断点
- 下一篇: Centos7手动安装OpenStack