在ASP中利用XML打包指定文件夹并上传到WEB目录中自行解包的方法
以前写的,发在
http://www.xml.org.cn/dispbbs.asp?boardID=32&ID=23966
现在加在自己的BLOG中
------------------------------------
前两天下了个Z-BLOG,发现其安装程序有点意思,只有两个文件,一个数据文件,XML格式的,一个解包程序
此程序仿照Z-BLOG的安装程序数据文件逆向写出
希望对那些不能批量上传文件的网友有帮助
'========================
'文件1
'Pack.asp
'更改Cpathname这一变量
'将在当前目录生成一个DATA.XML文件
'将DATA.XML及文件2(install.asp)上传至WEB根目录
'运行install.asp解包
'手动删除以上两个文件
'========================
<%OptionExplicit%>
<%OnErrorResumeNext%>
<%
Server.ScriptTimeout=99999999
dimCpathname
dimstartime,endtime
'在此更改要打包文件夹的路径
Cpathname="F:WEBsymr"
startime=timer()
functionbianli(path)
dimdoc
dimfso'fso对象
dimobjFolder'文件夹对象
dimobjSubFolders'子文件夹集合
dimobjSubFolder'子文件夹对象
dimobjFiles'文件集合
dimobjFile'文件对象
dimobjStream
dimpathname,TextStream,pp,Xfolder,Xfpath,Xfile,Xpath,Xstream
setfso=server.CreateObject("scripting.filesystemobject")
setobjFolder=fso.GetFolder(path)'创建文件夹对象
Response.Writepath
Response.flush
Setdoc=Server.CreateObject("MSxml2.DOMDocument")
doc.loadServer.MapPath("data.xml")
doc.async=false
'写入每个文件夹路径
setXfolder=doc.SelectSingleNode("//z-blog").AppendChild(doc.CreateElement("folder"))
SetXfpath=Xfolder.AppendChild(doc.CreateElement("path"))
Xfpath.text=replace(path,Cpathname,"")
setobjFiles=objFolder.Files
foreachobjFileinobjFiles
Response.Write"
---"
pp=path&""&objFile.name
Response.Writepp&"
"
Response.flush
'================================================
'写入文件的路径及文件内容
setXfile=doc.SelectSingleNode("//z-blog").AppendChild(doc.CreateElement("file"))
SetXpath=Xfile.AppendChild(doc.CreateElement("path"))
Xpath.text=replace(pp,Cpathname,"")
'创建文件流读入文件内容,并写入XML文件中
SetobjStream=Server.CreateObject("ADODB.Stream")
objStream.Type=1
objStream.Open()
objStream.LoadFromFile(pp)
objStream.position=0
SetXstream=Xfile.AppendChild(doc.CreateElement("stream"))
Xstream.SetAttribute"xmlns:dt","urn:schemas-microsoft-com:datatypes"
'文件内容采用二制方式存放
Xstream.dataType="bin.base64"
Xstream.nodeTypedValue=objStream.Read()
setobjStream=nothing
setXpath=nothing
setXstream=nothing
setXfile=nothing
'================================================
next
Response.Write"<p>"
doc.saveserver.mappath("data.xml")
setXfpath=nothing
setXfolder=nothing
setdoc=nothing
'创建的子文件夹对象
setobjSubFolders=objFolder.Subfolders
'调用递归遍历子文件夹
foreachobjSubFolderinobjSubFolders
pathname=path+""+objSubFolder.name
bianli(pathname)
next
setobjFolder=nothing
setobjSubFolders=nothing
setfso=nothing
endfunction
dimdoc,objPI
'创建一个空的XML文件,为写入文件作准备
Setdoc=Server.CreateObject("MSxml2.DOMDocument")
doc.async=false
setobjPI=doc.createProcessingInstruction("xml","version='1.0'encoding='UTF-8'")
doc.insertBeforeobjPI,doc.childNodes(0)
doc.appendChild(doc.CreateElement("z-blog"))
doc.saveserver.mappath("data.xml")
setobjPI=nothing
setdoc=nothing
bianli(Cpathname)
endtime=timer()
%>
页面执行时间:<%=FormatNumber((endtime-startime),3)%>秒
'文件1
'Pack.asp
'更改Cpathname这一变量
'将在当前目录生成一个DATA.XML文件
'将DATA.XML及文件2(install.asp)上传至WEB根目录
'运行install.asp解包
'手动删除以上两个文件
'========================
<%OptionExplicit%>
<%OnErrorResumeNext%>
<%
Server.ScriptTimeout=99999999
dimCpathname
dimstartime,endtime
'在此更改要打包文件夹的路径
Cpathname="F:WEBsymr"
startime=timer()
functionbianli(path)
dimdoc
dimfso'fso对象
dimobjFolder'文件夹对象
dimobjSubFolders'子文件夹集合
dimobjSubFolder'子文件夹对象
dimobjFiles'文件集合
dimobjFile'文件对象
dimobjStream
dimpathname,TextStream,pp,Xfolder,Xfpath,Xfile,Xpath,Xstream
setfso=server.CreateObject("scripting.filesystemobject")
setobjFolder=fso.GetFolder(path)'创建文件夹对象
Response.Writepath
Response.flush
Setdoc=Server.CreateObject("MSxml2.DOMDocument")
doc.loadServer.MapPath("data.xml")
doc.async=false
'写入每个文件夹路径
setXfolder=doc.SelectSingleNode("//z-blog").AppendChild(doc.CreateElement("folder"))
SetXfpath=Xfolder.AppendChild(doc.CreateElement("path"))
Xfpath.text=replace(path,Cpathname,"")
setobjFiles=objFolder.Files
foreachobjFileinobjFiles
Response.Write"
---"
pp=path&""&objFile.name
Response.Writepp&"
"
Response.flush
'================================================
'写入文件的路径及文件内容
setXfile=doc.SelectSingleNode("//z-blog").AppendChild(doc.CreateElement("file"))
SetXpath=Xfile.AppendChild(doc.CreateElement("path"))
Xpath.text=replace(pp,Cpathname,"")
'创建文件流读入文件内容,并写入XML文件中
SetobjStream=Server.CreateObject("ADODB.Stream")
objStream.Type=1
objStream.Open()
objStream.LoadFromFile(pp)
objStream.position=0
SetXstream=Xfile.AppendChild(doc.CreateElement("stream"))
Xstream.SetAttribute"xmlns:dt","urn:schemas-microsoft-com:datatypes"
'文件内容采用二制方式存放
Xstream.dataType="bin.base64"
Xstream.nodeTypedValue=objStream.Read()
setobjStream=nothing
setXpath=nothing
setXstream=nothing
setXfile=nothing
'================================================
next
Response.Write"<p>"
doc.saveserver.mappath("data.xml")
setXfpath=nothing
setXfolder=nothing
setdoc=nothing
'创建的子文件夹对象
setobjSubFolders=objFolder.Subfolders
'调用递归遍历子文件夹
foreachobjSubFolderinobjSubFolders
pathname=path+""+objSubFolder.name
bianli(pathname)
next
setobjFolder=nothing
setobjSubFolders=nothing
setfso=nothing
endfunction
dimdoc,objPI
'创建一个空的XML文件,为写入文件作准备
Setdoc=Server.CreateObject("MSxml2.DOMDocument")
doc.async=false
setobjPI=doc.createProcessingInstruction("xml","version='1.0'encoding='UTF-8'")
doc.insertBeforeobjPI,doc.childNodes(0)
doc.appendChild(doc.CreateElement("z-blog"))
doc.saveserver.mappath("data.xml")
setobjPI=nothing
setdoc=nothing
bianli(Cpathname)
endtime=timer()
%>
页面执行时间:<%=FormatNumber((endtime-startime),3)%>秒
'=================================
'文件2
'install.asp
'此文件改自z-blog安装文件
'=================================
<%@CODEPAGE=65001%>
<%OptionExplicit%>
<%OnErrorResumeNext%>
<%Response.Charset="UTF-8"%>
<html>
<head>
<title>文件解包程序</title>
</head>
<body>
<textareaname="content"cols="90"rows="20"style="border:0px;overflow:auto;border-width:0px;width:100%;background-color:#E8F3FF;"scrolling="auto">
<%
DimstrLocalPath
'得到当前文件夹的物理路径
strLocalPath=Left(Request.ServerVariables("PATH_TRANSLATED"),InStrRev(Request.ServerVariables("PATH_TRANSLATED"),""))
DimstrDbPath
DimobjXmlFile
DimobjNodeList
DimobjFSO
DimobjStream
Dimi,j
SetobjXmlFile=Server.CreateObject("Microsoft.XMLDOM")
objXmlFile.load(Server.MapPath("data.xml"))
IfobjXmlFile.readyState=4Then
IfobjXmlFile.parseError.errorCode=0Then
SetobjNodeList=objXmlFile.documentElement.selectNodes("//folder/path")
SetobjFSO=CreateObject("Scripting.FileSystemObject")
j=objNodeList.length-1
Fori=0Toj
IfobjFSO.FolderExists(strLocalPath&objNodeList(i).text)=FalseThen
objFSO.CreateFolder(strLocalPath&objNodeList(i).text)
EndIf
Response.Write"创建目录"&objNodeList(i).text&vbCrlf
Response.Flush
Next
SetobjNodeList=objXmlFile.documentElement.selectNodes("//file/path")
j=objNodeList.length-1
Fori=0Toj
SetobjStream=CreateObject("ADODB.Stream")
WithobjStream
.Type=1
.Open
.WriteobjNodeList(i).nextSibling.nodeTypedvalue
.SaveToFilestrLocalPath&objNodeList(i).text,2
Response.Write"释放文件"&objNodeList(i).text&vbCrlf
Response.Flush
.Close
EndWith
SetobjStream=Nothing
Next
EndIf
EndIf
%>
</textarea>
<%response.write"<script>alert('文件解包完毕!');</script>"%>
'文件2
'install.asp
'此文件改自z-blog安装文件
'=================================
<%@CODEPAGE=65001%>
<%OptionExplicit%>
<%OnErrorResumeNext%>
<%Response.Charset="UTF-8"%>
<html>
<head>
<title>文件解包程序</title>
</head>
<body>
<textareaname="content"cols="90"rows="20"style="border:0px;overflow:auto;border-width:0px;width:100%;background-color:#E8F3FF;"scrolling="auto">
<%
DimstrLocalPath
'得到当前文件夹的物理路径
strLocalPath=Left(Request.ServerVariables("PATH_TRANSLATED"),InStrRev(Request.ServerVariables("PATH_TRANSLATED"),""))
DimstrDbPath
DimobjXmlFile
DimobjNodeList
DimobjFSO
DimobjStream
Dimi,j
SetobjXmlFile=Server.CreateObject("Microsoft.XMLDOM")
objXmlFile.load(Server.MapPath("data.xml"))
IfobjXmlFile.readyState=4Then
IfobjXmlFile.parseError.errorCode=0Then
SetobjNodeList=objXmlFile.documentElement.selectNodes("//folder/path")
SetobjFSO=CreateObject("Scripting.FileSystemObject")
j=objNodeList.length-1
Fori=0Toj
IfobjFSO.FolderExists(strLocalPath&objNodeList(i).text)=FalseThen
objFSO.CreateFolder(strLocalPath&objNodeList(i).text)
EndIf
Response.Write"创建目录"&objNodeList(i).text&vbCrlf
Response.Flush
Next
SetobjNodeList=objXmlFile.documentElement.selectNodes("//file/path")
j=objNodeList.length-1
Fori=0Toj
SetobjStream=CreateObject("ADODB.Stream")
WithobjStream
.Type=1
.Open
.WriteobjNodeList(i).nextSibling.nodeTypedvalue
.SaveToFilestrLocalPath&objNodeList(i).text,2
Response.Write"释放文件"&objNodeList(i).text&vbCrlf
Response.Flush
.Close
EndWith
SetobjStream=Nothing
Next
EndIf
EndIf
%>
</textarea>
<%response.write"<script>alert('文件解包完毕!');</script>"%>
本文地址:http://www.45fan.com/dnjc/69933.html