ASP _xml example of using XML to package Web site files

Source: Internet
Author: User
Tags create directory flush pack servervariables unpack xpath
This method can pack the entire folder into the XML file, the XML file and the file and the package files together, run unpack files can be released from the original file, so we can pack the site to the virtual host, and then run unpack files on it. I chose a small number of files in the local test, I do not know how efficient the implementation of the file in many cases.
In fact, the idea of implementation is also very simple, mainly using the XML file can be stored in binary data principle. Interested friends can download the following attachment study!!
Unpack files
Copy Code code as follows:

<% @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 ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Cloud-dwelling community--File unpack program _www.jb51.net</title>

<body>
<%
Dim Strlocalpath
' Get the physical path to the current folder
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 "Create Directory" & 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 "Release File" & 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 "File Unpack complete"
%>
</body>

pack.asp Packaged Files
Copy Code code as follows:

<% @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 ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Packaging Program _ Cloud Habitat Community _www.jb51.net</title>

<body>
<%
Dim Zippathdir,zippathfile
Dim Startime,endtime
' Change the path of the folder you want to package here
Zippathdir = "D:\testasp\dictionary\xmlPacked\scrollColor"
Zippathfile = "Update.xml"
If Right (zippathdir,1) <> "\" then zippathdir=zippathdir& "\"
' Start packing
Createxml (Zippathfile)
' Traverse all files and folders within the directory
Sub LoadData (Dirpath)
Dim xmldoc
Dim fso ' FSO Object
Dim objfolder ' Folder Object
Dim objsubfolders ' Subfolder Collection
Dim Objsubfolder ' Subfolder Object
Dim objfiles ' File Collection
Dim objfile ' File Object
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) ' Create Folder object

Response.Write Dirpath
Response.Flush

Set xmldoc = Server.CreateObject ("Microsoft.XMLDOM")
Xmldoc.load Server.MapPath (Zippathfile)
Xmldoc.async=false

' Write each folder path
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
'================================================
' Write path to file and contents of file
Set xfile = Xmldoc.selectsinglenode ("//root"). AppendChild (xmldoc.createelement ("file"))
Set Xpath = Xfile.appendchild (xmldoc.createelement ("path"))
Xpath.text = replace (Pathnamestr,zippathdir, "")
' Create a file stream to read the contents of the file and write to the XML file
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"
' File contents are stored in two ways
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

' Subfolder object created by
Set Objsubfolders=objfolder.subfolders
' Calls recursive traversal of subfolders
For each objsubfolder in Objsubfolders
Pathname = Dirpath & objsubfolder.name & "\"
LoadData (Pathname)
Next
Set objfolder=nothing
Set objsubfolders=nothing
Set fso=nothing

End Sub

' Create an empty XML file to prepare for writing to the file
Sub Createxml (FilePath)
' Program Start execution time
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)
' Program End time
Endtime=timer ()
Response. Write ("Page Execution Time:" & FormatNumber ((Endtime-startime), 3) & "SEC")
End Sub
%>
</body>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.