用ASP實(shí)現(xiàn)在線壓縮與解壓縮功能代碼
先上一個(gè)完整代碼
<!-- Name -ASP在線解壓,壓縮工具 copyright -腳本之家(www.dbjr.com.cn) --> <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> <title>歡迎使用ASP在線解壓,壓縮工具</title> <body> <TABLE border=0 width=80% align=center cellspacing=1 cellpadding=3 style="FONT-FAMILY: Verdana;font-size:14px;BORDER: #000000 1px solid;background-color:#f7f7f7"> <tr> <th width="100%" height="24" bgcolor="#33CCFF"> </th> </tr> <tr> <td bgcolor="#33CCFF"> <% Server.ScriptTimeout=99999 Dim winrar,cmddir Winrar="C:\Program Files\WinRAR\Winrar.exe" '壓縮文件(Winrar)的地址 cmddir="%windir%\system32\cmd.exe" 'cmd.exe(命令提示符)的地址 user="csisp" '本程序的用戶名 pwd="csisp" '本程序的登陸、壓縮、解壓縮密碼,請(qǐng)大家務(wù)必修改(至少8位以上的組合密碼才夠安全!) if request.Form("user")=user and request.Form("pwd")=pwd then response.write "本文件夾路徑:"&Server.Mappath(".")&"<br>" from=request.Form("from") where=request.Form("where") if from<>"" and where<>"" then Dim a,b,Shell,Runing,Runcode,Cmd if instr(where,":")=0 then a=Server.mappath(""&where&"") else a=where if instr(from,":")=0 then b=Server.mappath(""&from&"") else b=from 'response.Write b if right(b,1)<>"\" and left(right(b,4),1)<>"." then b=b&".rar" On Error Resume Next Set Shell = Server.CreateObject("WScript.Shell") if request.QueryString("action")=1 then '解壓縮 if not ReportFileStatus(b)then Response.Write("沒有找到 "&b&"可能不存在!"):Response.End() Runing= cmddir&" /c """&winrar&""" x -ibck -t -y -o+ -p"&pwd&" " '設(shè)置運(yùn)行解壓縮的命令。 Cmd=Runing&b&" "&a&"\" elseif request.QueryString("action")=0 then '壓縮文件 if (not ReportFileStatus(a)) and (not ReportFolderStatus(a)) then Response.Write("沒有找到 "&a&"可能不存在!"):Response.End() Cmd= cmddir&" /c del /f /q "&b Runcode = Shell.Run(Cmd,1,True) Runing= cmddir&" /c """&winrar&""" a -ibck -y -ep -o+ -p"&pwd&" " '壓縮。 Cmd=Runing&b&" "&a else '刪除文件 Cmd= cmddir&" /c del /f /q "&b end if Runcode = Shell.Run(Cmd,1,True) Runing = Shell.Run(cmddir&" /c taskkill /im winrar.exe",1,false) Runing = Shell.Run(cmddir&" /c exit",1,false) Set Shell=nothing ErrInfo %> <%else%> <form name="frm" method="post" action="?action=1" style="BORDER: #d9d9d9 1px solid;background-color:#f7f7f7"> --------------------------------------<strong>解壓縮文件</strong>--------------------------------------- <br> <br> 請(qǐng)輸入rar壓縮文件地址: <input name="from" value="1.rar" size="50"><br> 解壓到:<input name="where" value="." size="50"> 文件夾請(qǐng)使用絕對(duì)路徑且在最后加 "\" <br> <br> <input name="submit" type="submit" value=" 解 壓 "><input type="hidden" name="user" value="<%=request.Form("user")%>"> <input type="hidden" name="pwd" value="<%=request.Form("pwd")%>"> </FORM> <form name="frm" method="post" action="?action=0" style="BORDER: #d9d9d9 1px solid;background-color:#f7f7f7"> --------------------------------------<strong>壓縮文件</strong>---------------------------------------<br> <br> 請(qǐng)?jiān)诖溯斎肽獕嚎s文件地址: <input name="where" value="./data/dvbbs7#.mdb" size="50"> 您也可以輸入文件夾<br> <br> 存放路徑及新文件名:<input name="from" value="../data/1.rar" size="50"> 自動(dòng)覆蓋同名文件<br> <br> <input name="submit" type="submit" value=" 壓 縮 "><input type="hidden" name="user" value="<%=request.Form("user")%>"> <input type="hidden" name="pwd" value="<%=request.Form("pwd")%>"></FORM> <% ErrInfo end if else login() end if Sub ErrInfo if not isempty(Runcode) and Runcode=0 Then Response.Write("操作成功執(zhí)行,您提交的操作如下:<br>"& Cmd) elseif not isempty(Runcode) then Response.Write("操作執(zhí)行失?。】赡苣臋?quán)限不夠或者該程序無法在DOS(命令提示符)下運(yùn)行,您提交的操作如下:<br>" & Cmd) else end if If Err Then Response.Write "<br>"&err.description err.Clear End If %> <form name="frm" method="post" action="?action=2" style="BORDER: #d9d9d9 1px solid;background-color:#f7f7f7"> --------------------------------------<strong>刪除文件</strong>---------------------------------------<br> <br> 請(qǐng)輸入要?jiǎng)h除文件地址:<input name="from" size="50" value=<%=from%>> 文件夾請(qǐng)使用絕對(duì)路徑且在最后加“\” <br> <input type="hidden" name="where" value=<%if where<>"" then response.Write where else response.Write "."%>> <br> <input name="submit" type="submit" value=" 刪 除 "> <input type="hidden" name="user" value="<%=request.Form("user")%>"> <input type="hidden" name="pwd" value="<%=request.Form("pwd")%>"> </FORM> <br> <% End Sub Function ReportFileStatus(filespec) Dim fso Set fso = CreateObject("Scripting.FileSystemObject") ReportFileStatus=false If (fso.FileExists(filespec)) Then ReportFileStatus = true Set fso =nothing End Function Function ReportFolderStatus(fldr) Dim fso Set fso = CreateObject("Scripting.FileSystemObject") ReportFolderStatus=false If (fso.FolderExists(fldr)) Then ReportFolderStatus = true Set fso =nothing End Function Sub login() %> <form name="frm" method="post" action="?action=2" style="BORDER: #d9d9d9 1px solid;background-color:#f7f7f7"> <p>--------------------------------------<strong>登陸系統(tǒng)</strong>---------------------------------------<br> <br> 用戶名: <input name="user" value="csisp"> </p> 密 碼: <input name="pwd" type="password" id="pwd"> <br> <br> <input name="submit" type="submit" value=" 登陸 "> </FORM> <%End sub%> </td> </tr> <tr> <td height="22" align="center" bgcolor="#FFFFFF"> Copyright ©2008 <a href="http://www.dbjr.com.cn">腳本之家</a></td> </tr> </table> </body>
使用方法:
1.直接上傳到服務(wù)器上(虛擬主機(jī))的任何目錄下(最好是根目錄)
2.用瀏覽器訪問Winrar.asp文件地址
3.輸入帳號(hào)和密碼 (默認(rèn)帳號(hào): csisp 默認(rèn)密碼: csisp)
4.輸入Winrar文件的地址(*.RAR) 確認(rèn)后即可解壓
5.輸入您想壓縮文件的地址,確認(rèn)后即可壓縮!
特別提示: 文件地址是本W(wǎng)inrar系統(tǒng)的相對(duì)地址!
使用本程序的服務(wù)器必須連安裝了WinRar (一般的服務(wù)器都安裝)
本程序的默認(rèn)程序地址適合99%以上的服務(wù)器(虛擬主機(jī))
如果地址不對(duì),可以自己修改,地址為您安裝WINRAR的地址(虛擬主機(jī)使用者請(qǐng)聯(lián)系您虛擬主機(jī)的客服)
下面一些細(xì)節(jié)說明與補(bǔ)充
一、問題的提出
隨著互連網(wǎng)的發(fā)展,網(wǎng)站的數(shù)量以驚人的數(shù)字增加。網(wǎng)站的作用除了給廣大網(wǎng)友們提供信息資訊服務(wù)外,還應(yīng)該成為網(wǎng)友們上傳與下載文件的場(chǎng)所。在上傳與下載文件的過程中,傳輸時(shí)間是關(guān)鍵,這就要求有較快的傳輸速度。在傳輸速度固定不變或是上下變動(dòng)不大的情況下,盡量減小傳輸文件的體積,是一個(gè)可行的辦法:上傳文件的時(shí)候,先將要上傳的文件用WINRAR壓縮,上傳成功后在網(wǎng)站內(nèi)通過程序?qū)崿F(xiàn)解壓縮;下載文件的時(shí)候,先將要下載的文件在網(wǎng)站內(nèi)通過程序?qū)崿F(xiàn)壓縮然后再下載。本文就針對(duì)該問題的解決進(jìn)行探討。
二、方法與說明
首先要上傳一個(gè)WINRAR的解壓程序,就是WINRAR自己的解壓程序,只需要它的核心程序RAR.EXE這個(gè)文件就可以了,在WINRAR的安裝目錄里可以找到。然后要上傳一個(gè)執(zhí)行RAR.EXE的程序 CMD.EXE 這個(gè)是WINDOWS操作系統(tǒng)里的程序,在系統(tǒng)盤WINNT/SYSTEM32文件夾中可以找到。上傳完這兩個(gè)文件后,就需要編寫一些程序代碼來實(shí)現(xiàn)壓縮與解壓縮。
不妨將實(shí)現(xiàn)壓縮功能的文件取名為ZIP.ASP,實(shí)現(xiàn)解壓縮功能的文件取名為UNZIP.ASP。
程序代碼中包含了WINRAR這個(gè)應(yīng)用程序的命令行語法,下面簡單介紹一下:
WinRAR <命令> -<開關(guān)1> -<開關(guān)N> <壓縮文件> <文件...> <@列表文件...> <解壓路徑/>
命令 要 WinRAR 運(yùn)行的字符組合代表功能。
開關(guān) 切換操作指定類型,壓縮強(qiáng)度,壓縮文件類型,等等的定義。
壓縮文件 要處理的壓縮文件名。
文件 要處理的文件名。
列表文件 列表文件是包含要處理文件名稱的純文本。文件名應(yīng)該在第一卷啟動(dòng)??梢栽诹斜砦募惺褂?/字符后添加注釋。例如,你可以包含兩列字符串創(chuàng)建 backup.lst:c:/work/doc/*.txt //備份文本文檔 c:/work/image/*.bmp //備份圖片c:/work/misc并接著運(yùn)行: winrar a backup @backup.lst你可以在命令行中同時(shí)指定普通的文件名和列表文件名。
解壓路徑 只與命令 e 和 x ,搭配使用。指出解壓文件添加的位置。如果文件夾不存在時(shí),會(huì)自動(dòng)創(chuàng)建。
字母命令列表:
a 添加文件到壓縮文件 | c 添加壓縮文件注釋 |
d 從壓縮文件刪除文件 | e 從壓縮文件解壓壓縮,忽略路徑 |
f 刷新壓縮文件中的文件 | i 在壓縮文件中查找字符串 |
k 鎖定壓縮文件 | m 移動(dòng)文件和文件夾到壓縮文件 |
r 修復(fù)受損的壓縮文件 | rc 重建丟失的卷 |
rn 重命名壓縮文件 | rr[N] 添加數(shù)據(jù)恢復(fù)記錄 |
rv[N] 創(chuàng)建恢復(fù)卷 | s[name] 轉(zhuǎn)換壓縮文件成為自解壓文件類型 |
s- 刪除自解壓模塊 | t 測(cè)試壓縮文件 |
u 從壓縮文件中更新文件 | x 以完整路徑名稱從壓縮文件解壓壓縮 |
其實(shí)程序代碼中的關(guān)鍵之處就是用Server.CreateObject("Wscript.Shell")來執(zhí)行CMD.EXE,CMD.EXE運(yùn)行RAR.EXE通過WINRAR的命令來執(zhí)行解壓縮文件與壓縮文件的。
三、程序清單
ZIP.ASP程序清單:
<% 'main文件夾中包含cmd.exe rar.exe 例如:要壓縮的文件(*.mdb) '壓縮后的存放目錄為main/data.rar on error resume next unzip_path=Server.mappath("main")&"/" Set WshShell = server.CreateObject("Wscript.Shell") IsSuccess = WshShell.Run ("winrar a "&unzip_path&"data "&unzip_path&"*.mdb",1, False) 'WinRAR <命令> -<開關(guān)1> -<開關(guān)N> <壓縮文件> <文件...> <@列表文件...> <解壓路徑/> '命令: A - 添加到壓縮文件中 if IsSuccess = 0 Then Response.write " 命令成功執(zhí)行!" else Response.write " 命令執(zhí)行失?。?quán)限不夠或者該程序無法運(yùn)行" end if if err.number <> 0 then Response.Write "<p>錯(cuò)誤號(hào)碼:" & Err.number Response.Write "<p>原因:" & Err.description Response.Write "<p>錯(cuò)誤來源:" & Err.Source Response.Write end if %>
UNZIP.ASP程序清單:
<% 'main文件夾中包含cmd.exe rar.exe 要解壓縮的文件(*.rar) '解壓縮后的存放目錄為main on error resume next unzip_path=Server.mappath("main")&"/" Set WshShell = server.CreateObject("Wscript.Shell") IsSuccess = WshShell.Run ("winrar x -r -o+ "&unzip_path&"*.rar "&unzip_path&"",1, False) 'WinRAR <命令> -<開關(guān)1> -<開關(guān)N> <壓縮文件> <文件...> <@列表文件...> <解壓路徑/> '命令: X - 從壓縮文件中全路徑解壓文件 '開關(guān): -R - 連同子文件夾 '開關(guān): -O+ - 覆蓋已經(jīng)存在的文件 '開關(guān): -O- - 不覆蓋已經(jīng)存在的文件 if IsSuccess = 0 Then Response.write " 命令成功執(zhí)行!" else Response.write " 命令執(zhí)行失??!權(quán)限不夠或者該程序無法運(yùn)行" end if if err.number <> 0 then Response.Write "<p>錯(cuò)誤號(hào)碼:" & Err.number Response.Write "<p>原因:" & Err.description Response.Write "<p>錯(cuò)誤來源:" & Err.Source Response.Write end if %>
四、 結(jié)論
本文通過使用RAR.EXE和 CMD.EXE這兩個(gè)文件,在ASP編程中實(shí)現(xiàn)在線壓縮與解壓縮網(wǎng)站中的文件的功能。實(shí)現(xiàn)方法簡單,程序代碼少,可供實(shí)用編程時(shí)借鑒使用。尤其是在擁有自己的服務(wù)器的情況下極其實(shí)用和方便。以上程序在Windows2000Sever及IIS5.0下運(yùn)行通過。
相關(guān)文章
使用AJAX實(shí)現(xiàn)UTF8編碼表單提交到GBK編碼腳本無亂碼的解決方法
這篇文章主要介紹了用AJAX實(shí)現(xiàn),多個(gè)不同編碼的項(xiàng)目(GBK,UTF8)互相通過 POST/GET 匯總表單數(shù)據(jù)。最終實(shí)現(xiàn) 使用同一個(gè)頁面接收 不同編碼的 表單(GBK,UTF8),特別適合 JS 采集項(xiàng)目,需要的朋友可以參考下2020-09-09Asp 操作Access數(shù)據(jù)庫時(shí)出現(xiàn)死鎖.ldb的解決方法
在一天某個(gè)時(shí)間段內(nèi)打不開,等待好幾分鐘就會(huì)說asp腳本超時(shí),但FTP可以登陸進(jìn)去,查看數(shù)據(jù)庫目錄時(shí),發(fā)現(xiàn)與數(shù)據(jù)庫(我的數(shù)據(jù)庫是access數(shù)據(jù)庫)同名的記錄鎖定信息文件 (.ldb文件)一直都在那里2009-04-04asp 實(shí)現(xiàn)對(duì)SQL注入危險(xiǎn)字符進(jìn)行重編碼處理的函數(shù)
asp 實(shí)現(xiàn)對(duì)SQL注入危險(xiǎn)字符進(jìn)行重編碼處理的函數(shù)...2007-08-08巧用FileSystem組件實(shí)現(xiàn)WEB應(yīng)用中的本地特定打印的方法
巧用FileSystem組件實(shí)現(xiàn)WEB應(yīng)用中的本地特定打印的方法...2007-04-04asp中獲取當(dāng)前頁面的地址與參數(shù)的函數(shù)代碼
asp中獲取當(dāng)前頁面的地址與參數(shù)的函數(shù)代碼,經(jīng)常需要用得到,特整理下,方便需要的朋友。2011-01-01