猫七原创ASP压缩解压系统-WEBZip,原理即遍历当前目录的所有文件和文件夹,将结果保存到MDB数据库中。现在已经实现了ASP无需任何插件,仅用Access数据库。
全部代码如下:
<%
Server.ScriptTimeout=9999
Response.Buffer=false
Dim WEBZip_Path,WEBZip_Date,WEBZip_CMD,WEBZip_File
Dim WEBZip_FSO,WebZip_Conn,WEBZip_Rs,WEBZip_ADOStrem
'压缩相关
'================================================================
Function findFiles(p)
savePath(p)
for each s in p.subfolders
findFiles(s)
next
for each f in p.files
saveFile(f)
next
End Function
Function savePath(p)
If trim(replace(p,WEBZip_Path,""))="" Then Exit Function
RunSqlOne("insert into [p]([p_na],[p_pa],[p_cn])values('"&p.name&"','"&replace(p,WEBZip_Path,"")&"',"&p.subfolders.count&")")
echo "找到路径:"&p
End Function
Function saveFile(f)
If instr(f,WEBZip_File)>0 Then Exit Function
RunSqlOne("insert into [f]([f_na],[f_pa],[f_sz])values('"&f.name&"','"&replace(f,WEBZip_Path,"")&"',"&f.size&")")
SaveFileContent(replace(f,WEBZip_Path,""))
echo "压缩文件:"&f
End Function
'写入文件内容
Function SaveFileContent(f)
fl=WEBZip_FSO.getfile(Server.Mappath(f))
WEBZip_Rs.Open "select * from [f] where [f_pa]='"&f&"'",WEBZip_Conn,3,2
With WEBZip_ADOStrem
.Type = 1
.Open
.LoadFromFile fl
WEBZip_Rs("f_co") = .Read
.Close
End With
WEBZip_Rs.update
WEBZip_Rs.Close
End Function
'解压缩相关
'================================================================
Function Expand()
WEBZip_Rs.open "select * from [p] order by [p_id] asc",WEBZip_Conn,1,1
Do While Not WEBZip_Rs.eof
Call ExpandPath(WEBZip_Rs("p_pa"))
WEBZip_Rs.MoveNext
Loop
WEBZip_Rs.Close
WEBZip_Rs.open "select * from [f] order by [f_id] asc",WEBZip_Conn,1,1
Do While Not WEBZip_Rs.eof
if WEBZip_Rs("f_sz")< =1 then
Call CreateFile(WEBZip_Rs("f_pa"))
else
Call ExpandFile(WEBZip_Rs("f_pa"),WEBZip_Rs("f_id"))
end if
WEBZip_Rs.MoveNext
Loop
WEBZip_Rs.Close
End Function
Function ExpandPath(p)
p=Server.Mappath(p)
If Not WEBZip_FSO.FolderExists(p) Then
WEBZip_FSO.CreateFolder(p)
echo "解压目录:"&p
End if
End Function
Function ShowFile()
id=Request.QueryString("id")
if not isNumeric(id) or trim(id)="" then Response.end
WEBZip_Rs.open "select [f_co],[f_sz] from [f] where [f_id]="&id,WEBZip_Conn,1,1
if not WEBZip_Rs.eof then
WEBZip_RsDB=WEBZip_Rs(0).GetChunk(WEBZip_Rs(1))
End if
WEBZip_Rs.Close
Response.BinaryWrite WEBZip_RsDB
End Function
Function ExpandFile(f,id)
fl=Server.Mappath(f)
Set Http=Server.CreateObject("MSXML2.XMLHTTP")
randomize
Http.open "GET","http://"&Request.Servervariables("SERVER_NAME")&":"&Request.Servervariables("SERVER_PORT")&Request.Servervariables("SCRIPT_NAME")&"?cmd=show&id="&id&"&f="&now()&rnd&f,False
Http.send()
if Http.readystate<>4 then
exit Function
end if
With WEBZip_ADOStrem
.Type = 1
.Mode = 3
.Open
.Write Http.ResponseBody
.SaveToFile Server.Mappath(f)
.Close
End With
echo "解压文件:"&f
End Function
Function CreateFile(f)
fl=Server.Mappath(f)
WEBZip_FSO.CreateTextFile(fl)
echo "解压文件:"&f
End function
'菜单
'================================================================
Function ShowMenu()
Response.write "<input id=""status"" style=""width:640px;background:#FFF;border:none;"" readonly=""readonly""/><hr />"
Call AddLink("压缩","tozip")
Call AddLink("解压","unzip")
Call AddLink("卸载[暂不支持]","uninstall")
End Function
Function AddLink(t,l)
Response.write "<a href=""?cmd="&CommandToQuery(l)&""">"&t&"</a> "
End Function
Function RunSqlOne(sql)
WEBZip_Rs.Open sql,WebZip_Conn,3,2
End Function
sub echo(t)
'Response.write t&vbCrlf
Response.write "<script>document.getElementById(""status"").value='"&replace(t,"\","\\")&"';</script>"
'Response.flush
End Sub
'扩展命令
' tozip 压缩
' unzip 解压
' 即将支持
' tozip -t fr 小文件模式
' tozip -t mu -f 100 多文件模式,指定-f为100,每次处理100个文件
' 扩展命令转换
' tozip -t fr -f 100 < ==> tozip&t=fr&f=100
Function CommandToQuery(c)
CommandToQuery=replace(replace(c," -","&")," ","=")
End Function
Function QueryToCommand(q)
QueryToCommand=replace(replace(q,"&"," -"),"="," ")
End Function
'初始化
'================================================================
'WEBZip 安装路径
WEBZip_File=Server.Mappath(Request.Servervariables("SCRIPT_NAME"))
WEBZip_Path=Server.Mappath("./")
WEBZip_Date=WEBZip_File&".webzip"
WEBZip_CMD=Request.QueryString("cmd")
set WEBZip_FSO=Server.CreateObject("Scripting.FileSystemObject")
set WebZip_Conn=Server.CreateObject("ADODB.Connection")
WebZip_Conn.Open "provider=microsoft.jet.oledb.4.0;data source="&WEBZip_Date
set WEBZip_Rs=Server.CreateObject("ADODB.Recordset")
set WEBZip_ADOStrem=Server.CreateObject("ADODB.Stream")
'初始化菜单
Select Case WEBZip_CMD
Case "tozip"
Call ShowMenu()
Call findFiles(WEBZip_FSO.GetFolder(Server.Mappath("./")))
Case "unzip"
Call ShowMenu()
Call Expand()
Case "show"
Call ShowFile()
Case Else
Call ShowMenu()
End Select
%>