asp下利用xml打包网站文件
程序员文章站
2022-06-23 21:59:25
这个方法可以把整个文件夹打包到xml文件中,把这个xml文件文件和解包文件放在一起后,运行解包文件就可以把原来的文件释放出来,这样我们就可以把网站打包上传到虚拟主机,再运行...
这个方法可以把整个文件夹打包到xml文件中,把这个xml文件文件和解包文件放在一起后,运行解包文件就可以把原来的文件释放出来,这样我们就可以把网站打包上传到虚拟主机,再运行解包文件就可以了。我在本地测试之选择了少部分文件,不知在文件很多的情况执行效率如何。
其实实现的思路也很简单,主要利用的是xml文件可以存放二进制数据的原理。有兴趣的朋友可以下载下面的附件研究下!!
解包文件
<%@language="vbscript" codepage="65001"%>
<% option explicit %>
<% on error resume next %>
<% response.charset="utf-8" %>
<% server.scripttimeout=99999999 %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>——文件解包程序_www.jb51.net</title>
</head>
<body>
<%
dim strlocalpath
'得到当前文件夹的物理路径
strlocalpath=left(request.servervariables("path_translated"),instrrev(request.servervariables("path_translated"),"\"))
dim objxmlfile
dim objnodelist
dim objfso
dim objstream
dim i,j
set objxmlfile = server.createobject("microsoft.xmldom")
objxmlfile.load(server.mappath("update.xml"))
if objxmlfile.readystate=4 then
if objxmlfile.parseerror.errorcode = 0 then
set objnodelist = objxmlfile.documentelement.selectnodes("//folder/path")
set objfso = createobject("scripting.filesystemobject")
j=objnodelist.length-1
for i=0 to j
if objfso.folderexists(strlocalpath & objnodelist(i).text)=false then
objfso.createfolder(strlocalpath & objnodelist(i).text)
end if
response.write "创建目录" & objnodelist(i).text & "<br/>"
response.flush
next
set objfso = nothing
set objnodelist = nothing
set objnodelist = objxmlfile.documentelement.selectnodes("//file/path")
j=objnodelist.length-1
for i=0 to j
set objstream = createobject("adodb.stream")
with objstream
.type = 1
.open
.write objnodelist(i).nextsibling.nodetypedvalue
.savetofile strlocalpath & objnodelist(i).text,2
response.write "释放文件" & objnodelist(i).text & "<br/>"
response.flush
.close
end with
set objstream = nothing
next
set objnodelist = nothing
end if
end if
set objxmlfile = nothing
response.write "文件解包完毕"
%>
</body>
</html>
pack.asp 打包文件
<%@language="vbscript" codepage="65001"%>
<% option explicit %>
<% on error resume next %>
<% response.charset="utf-8" %>
<% server.scripttimeout=99999999 %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>文件打包程序__www.jb51.net</title>
</head>
<body>
<%
dim zippathdir,zippathfile
dim startime,endtime
'在此更改要打包文件夹的路径
zippathdir = "d:\testasp\dictionary\xmlpacked\scrollcolor"'
zippathfile = "update.xml"
if right(zippathdir,1)<>"\" then zippathdir=zippathdir&"\"
'开始打包
createxml(zippathfile)
'遍历目录内的所有文件以及文件夹
sub loaddata(dirpath)
dim xmldoc
dim fso 'fso对象
dim objfolder '文件夹对象
dim objsubfolders '子文件夹集合
dim objsubfolder '子文件夹对象
dim objfiles '文件集合
dim objfile '文件对象
dim objstream
dim pathname,textstream,pp,xfolder,xfpath,xfile,xpath,xstream
dim pathnamestr
response.write("=========="&dirpath&"==========<br>")
set fso=server.createobject("scripting.filesystemobject")
set objfolder=fso.getfolder(dirpath)'创建文件夹对象
response.write dirpath
response.flush
set xmldoc = server.createobject("microsoft.xmldom")
xmldoc.load server.mappath(zippathfile)
xmldoc.async=false
'写入每个文件夹路径
set xfolder = xmldoc.selectsinglenode("//root").appendchild(xmldoc.createelement("folder"))
set xfpath = xfolder.appendchild(xmldoc.createelement("path"))
xfpath.text = replace(dirpath,zippathdir,"")
set objfiles=objfolder.files
for each objfile in objfiles
if lcase(dirpath & objfile.name) <> lcase(request.servervariables("path_translated")) then
response.write "---<br/>"
pathnamestr = dirpath & "" & objfile.name
response.write pathnamestr & ""
response.flush
'================================================
'写入文件的路径及文件内容
set xfile = xmldoc.selectsinglenode("//root").appendchild(xmldoc.createelement("file"))
set xpath = xfile.appendchild(xmldoc.createelement("path"))
xpath.text = replace(pathnamestr,zippathdir,"")
'创建文件流读入文件内容,并写入xml文件中
set objstream = server.createobject("adodb.stream")
objstream.type = 1
objstream.open()
objstream.loadfromfile(pathnamestr)
objstream.position = 0
set xstream = xfile.appendchild(xmldoc.createelement("stream"))
xstream.setattribute "xmlns:dt","urn:schemas-microsoft-com:datatypes"
'文件内容采用二制方式存放
xstream.datatype = "bin.base64"
xstream.nodetypedvalue = objstream.read()
set objstream=nothing
set xpath = nothing
set xstream = nothing
set xfile = nothing
'================================================
end if
next
response.write "<p>"
xmldoc.save(server.mappath(zippathfile))
set xfpath = nothing
set xfolder = nothing
set xmldoc = nothing
'创建的子文件夹对象
set objsubfolders=objfolder.subfolders
'调用递归遍历子文件夹
for each objsubfolder in objsubfolders
pathname = dirpath & objsubfolder.name & "\"
loaddata(pathname)
next
set objfolder=nothing
set objsubfolders=nothing
set fso=nothing
end sub
'创建一个空的xml文件,为写入文件作准备
sub createxml(filepath)
'程序开始执行时间
startime=timer()
dim xmldoc,root
set xmldoc = server.createobject("microsoft.xmldom")
xmldoc.async = false
set root = xmldoc.createprocessinginstruction("xml","version='1.0' encoding='utf-8'")
xmldoc.appendchild(root)
xmldoc.appendchild(xmldoc.createelement("root"))
xmldoc.save(server.mappath(filepath))
set root = nothing
set xmldoc = nothing
loaddata(zippathdir)
'程序结束时间
endtime=timer()
response.write("页面执行时间:" & formatnumber((endtime-startime),3) & "秒")
end sub
%>
</body>
</html>
其实实现的思路也很简单,主要利用的是xml文件可以存放二进制数据的原理。有兴趣的朋友可以下载下面的附件研究下!!
解包文件
复制代码 代码如下:
<%@language="vbscript" codepage="65001"%>
<% option explicit %>
<% on error resume next %>
<% response.charset="utf-8" %>
<% server.scripttimeout=99999999 %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>——文件解包程序_www.jb51.net</title>
</head>
<body>
<%
dim strlocalpath
'得到当前文件夹的物理路径
strlocalpath=left(request.servervariables("path_translated"),instrrev(request.servervariables("path_translated"),"\"))
dim objxmlfile
dim objnodelist
dim objfso
dim objstream
dim i,j
set objxmlfile = server.createobject("microsoft.xmldom")
objxmlfile.load(server.mappath("update.xml"))
if objxmlfile.readystate=4 then
if objxmlfile.parseerror.errorcode = 0 then
set objnodelist = objxmlfile.documentelement.selectnodes("//folder/path")
set objfso = createobject("scripting.filesystemobject")
j=objnodelist.length-1
for i=0 to j
if objfso.folderexists(strlocalpath & objnodelist(i).text)=false then
objfso.createfolder(strlocalpath & objnodelist(i).text)
end if
response.write "创建目录" & objnodelist(i).text & "<br/>"
response.flush
next
set objfso = nothing
set objnodelist = nothing
set objnodelist = objxmlfile.documentelement.selectnodes("//file/path")
j=objnodelist.length-1
for i=0 to j
set objstream = createobject("adodb.stream")
with objstream
.type = 1
.open
.write objnodelist(i).nextsibling.nodetypedvalue
.savetofile strlocalpath & objnodelist(i).text,2
response.write "释放文件" & objnodelist(i).text & "<br/>"
response.flush
.close
end with
set objstream = nothing
next
set objnodelist = nothing
end if
end if
set objxmlfile = nothing
response.write "文件解包完毕"
%>
</body>
</html>
pack.asp 打包文件
复制代码 代码如下:
<%@language="vbscript" codepage="65001"%>
<% option explicit %>
<% on error resume next %>
<% response.charset="utf-8" %>
<% server.scripttimeout=99999999 %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>文件打包程序__www.jb51.net</title>
</head>
<body>
<%
dim zippathdir,zippathfile
dim startime,endtime
'在此更改要打包文件夹的路径
zippathdir = "d:\testasp\dictionary\xmlpacked\scrollcolor"'
zippathfile = "update.xml"
if right(zippathdir,1)<>"\" then zippathdir=zippathdir&"\"
'开始打包
createxml(zippathfile)
'遍历目录内的所有文件以及文件夹
sub loaddata(dirpath)
dim xmldoc
dim fso 'fso对象
dim objfolder '文件夹对象
dim objsubfolders '子文件夹集合
dim objsubfolder '子文件夹对象
dim objfiles '文件集合
dim objfile '文件对象
dim objstream
dim pathname,textstream,pp,xfolder,xfpath,xfile,xpath,xstream
dim pathnamestr
response.write("=========="&dirpath&"==========<br>")
set fso=server.createobject("scripting.filesystemobject")
set objfolder=fso.getfolder(dirpath)'创建文件夹对象
response.write dirpath
response.flush
set xmldoc = server.createobject("microsoft.xmldom")
xmldoc.load server.mappath(zippathfile)
xmldoc.async=false
'写入每个文件夹路径
set xfolder = xmldoc.selectsinglenode("//root").appendchild(xmldoc.createelement("folder"))
set xfpath = xfolder.appendchild(xmldoc.createelement("path"))
xfpath.text = replace(dirpath,zippathdir,"")
set objfiles=objfolder.files
for each objfile in objfiles
if lcase(dirpath & objfile.name) <> lcase(request.servervariables("path_translated")) then
response.write "---<br/>"
pathnamestr = dirpath & "" & objfile.name
response.write pathnamestr & ""
response.flush
'================================================
'写入文件的路径及文件内容
set xfile = xmldoc.selectsinglenode("//root").appendchild(xmldoc.createelement("file"))
set xpath = xfile.appendchild(xmldoc.createelement("path"))
xpath.text = replace(pathnamestr,zippathdir,"")
'创建文件流读入文件内容,并写入xml文件中
set objstream = server.createobject("adodb.stream")
objstream.type = 1
objstream.open()
objstream.loadfromfile(pathnamestr)
objstream.position = 0
set xstream = xfile.appendchild(xmldoc.createelement("stream"))
xstream.setattribute "xmlns:dt","urn:schemas-microsoft-com:datatypes"
'文件内容采用二制方式存放
xstream.datatype = "bin.base64"
xstream.nodetypedvalue = objstream.read()
set objstream=nothing
set xpath = nothing
set xstream = nothing
set xfile = nothing
'================================================
end if
next
response.write "<p>"
xmldoc.save(server.mappath(zippathfile))
set xfpath = nothing
set xfolder = nothing
set xmldoc = nothing
'创建的子文件夹对象
set objsubfolders=objfolder.subfolders
'调用递归遍历子文件夹
for each objsubfolder in objsubfolders
pathname = dirpath & objsubfolder.name & "\"
loaddata(pathname)
next
set objfolder=nothing
set objsubfolders=nothing
set fso=nothing
end sub
'创建一个空的xml文件,为写入文件作准备
sub createxml(filepath)
'程序开始执行时间
startime=timer()
dim xmldoc,root
set xmldoc = server.createobject("microsoft.xmldom")
xmldoc.async = false
set root = xmldoc.createprocessinginstruction("xml","version='1.0' encoding='utf-8'")
xmldoc.appendchild(root)
xmldoc.appendchild(xmldoc.createelement("root"))
xmldoc.save(server.mappath(filepath))
set root = nothing
set xmldoc = nothing
loaddata(zippathdir)
'程序结束时间
endtime=timer()
response.write("页面执行时间:" & formatnumber((endtime-startime),3) & "秒")
end sub
%>
</body>
</html>
推荐阅读