当前位置:asp编程网>技术教程>Xml教程>  正文

将asp程序制作成为安装版:xml打包和解包

2008-04-29 09:40:05   来源:asp学习网    作者:佚名   浏览量:5453   收藏

首先是打包(谈不上压缩,不过经常用ftp的情况下,经常这样打包会好很多的,ftp最怕的就是零碎文件太多,况且现在的网络情况...)

 

打包代码
 

  1. <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%> 
  2. <% Option Explicit %> 
  3. <% On Error Resume Next %> 
  4. <% Response.Charset="UTF-8" %> 
  5. <% Server.ScriptTimeout=99999999 %> 
  6. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  7. <html xmlns="http://www.w3.org/1999/xhtml">  
  8. <head>  
  9. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  10. <title>文件打包程序</title>  
  11. </head>  
  12. <body>  
  13. <% 
  14. Dim ZipPathDir, ZipPathFile, ZipFileExt 
  15. Dim startime, endtime 
  16. '此为默认当前文件夹 
  17.  
  18. ZipPathDir = Left(Request.ServerVariables("PATH_TRANSLATED"), InStrRev(Request.ServerVariables("PATH_TRANSLATED"), "\")) 
  19. '在此更改要打包文件夹的路径 
  20.  
  21. 'ZipPathDir="D:\MYWEB\WEBINFO" 
  22.  
  23. '生成的xml文件 
  24.  
  25. ZipPathFile = "update.xml" 
  26. '不进行打包的文件扩展名 
  27.  
  28. ZipFileExt = "db;bak" 
  29. If Right(ZipPathDir, 1)<>"\" Then ZipPathDir = ZipPathDir&"\" 
  30. '开始打包 
  31.  
  32. CreateXml(ZipPathFile) 
  33. '遍历目录内的所有文件以及文件夹 
  34.  
  35. Sub LoadData(DirPath) 
  36.     Dim XmlDoc 
  37.     Dim fso 'fso对象 
  38.  
  39.     Dim objFolder '文件夹对象 
  40.  
  41.     Dim objSubFolders '子文件夹集合 
  42.  
  43.     Dim objSubFolder '子文件夹对象 
  44.  
  45.     Dim objFiles '文件集合 
  46.  
  47.     Dim objFile '文件对象 
  48.  
  49.     Dim objStream 
  50.     Dim pathname, TextStream, pp, Xfolder, Xfpath, Xfile, Xpath, Xstream 
  51.     Dim PathNameStr 
  52.     response.Write("=========="&DirPath&"==========<br>"
  53.     Set fso = server.CreateObject("scripting.filesystemobject"
  54.     Set objFolder = fso.GetFolder(DirPath)'创建文件夹对象 
  55.  
  56.     Response.Write DirPath 
  57.     Response.flush 
  58.     Set XmlDoc = Server.CreateObject("Microsoft.XMLDOM"
  59.     XmlDoc.load(Server.MapPath(ZipPathFile)) 
  60.     XmlDoc.async = false 
  61.     '写入每个文件夹路径 
  62.  
  63.     Set Xfolder = XmlDoc.SelectSingleNode("//root").AppendChild(XmlDoc.CreateElement("folder")) 
  64.     Set Xfpath = Xfolder.AppendChild(XmlDoc.CreateElement("path")) 
  65.     Xfpath.text = Replace(DirPath, ZipPathDir, ""
  66.     Set objFiles = objFolder.Files 
  67.     For Each objFile in objFiles 
  68.         If LCase(DirPath & objFile.Name) <> LCase(Request.ServerVariables("PATH_TRANSLATED")) And LCase(DirPath & objFile.Name) <> LCase(DirPath & ZipPathFile) Then 
  69.             If ext(objFile.Name) Then 
  70.                 Response.Write "---<br/>" 
  71.                 PathNameStr = DirPath & "" & objFile.Name 
  72.                 Response.Write PathNameStr & "" 
  73.                 Response.flush 
  74.                 '================================================ 
  75.  
  76.                 '写入文件的路径及文件内容 
  77.  
  78.                 Set Xfile = XmlDoc.SelectSingleNode("//root").AppendChild(XmlDoc.CreateElement("file")) 
  79.                 Set Xpath = Xfile.AppendChild(XmlDoc.CreateElement("path")) 
  80.                 Xpath.text = Replace(PathNameStr, ZipPathDir, ""
  81.                 '创建文件流读入文件内容,并写入XML文件中 
  82.  
  83.                 Set objStream = Server.CreateObject("ADODB.Stream"
  84.                 objStream.Type = 1 
  85.                 objStream.Open() 
  86.                 objStream.LoadFromFile(PathNameStr) 
  87.                 objStream.position = 0 
  88.                 Set Xstream = Xfile.AppendChild(XmlDoc.CreateElement("stream")) 
  89.                 Xstream.SetAttribute "xmlns:dt""urn:schemas-microsoft-com:datatypes" 
  90.                 '文件内容采用二制方式存放 
  91.  
  92.                 Xstream.dataType = "bin.base64" 
  93.                 Xstream.nodeTypedValue = objStream.Read() 
  94.                 Set objStream = Nothing 
  95.                 Set Xpath = Nothing 
  96.                 Set Xstream = Nothing 
  97.                 Set Xfile = Nothing 
  98.                 '================================================ 
  99.  
  100.             End If 
  101.         End If 
  102.     Next 
  103.     Response.Write "<p>" 
  104.     XmlDoc.Save(Server.Mappath(ZipPathFile)) 
  105.     Set Xfpath = Nothing 
  106.     Set Xfolder = Nothing 
  107.     Set XmlDoc = Nothing 
  108.     '创建的子文件夹对象 
  109.  
  110.     Set objSubFolders = objFolder.SubFolders 
  111.     '调用递归遍历子文件夹 
  112.  
  113.     For Each objSubFolder in objSubFolders 
  114.         pathname = DirPath & objSubFolder.Name & "\" 
  115.         LoadData(pathname) 
  116.     Next 
  117.     Set objFolder = Nothing 
  118.     Set objSubFolders = Nothing 
  119.     Set fso = Nothing 
  120. End Sub 
  121. '创建一个空的XML文件,为写入文件作准备 
  122.  
  123. Sub CreateXml(FilePath) 
  124.     '程序开始执行时间 
  125.  
  126.     startime = Timer() 
  127.     Dim XmlDoc, Root 
  128.     Set XmlDoc = Server.CreateObject("Microsoft.XMLDOM"
  129.     XmlDoc.async = False 
  130.     Set Root = XmlDoc.createProcessingInstruction("xml""version='1.0' encoding='UTF-8'"
  131.     XmlDoc.appendChild(Root) 
  132.     XmlDoc.appendChild(XmlDoc.CreateElement("root")) 
  133.     XmlDoc.Save(Server.MapPath(FilePath)) 
  134.     Set Root = Nothing 
  135.     Set XmlDoc = Nothing 
  136.     LoadData(ZipPathDir) 
  137.     '程序结束时间 
  138.  
  139.     endtime = Timer() 
  140.     response.Write("页面执行时间:" & FormatNumber((endtime - startime), 3) & "秒"
  141. End Sub 
  142. '判断文件类型是否合法 
  143.  
  144. Function ext(filename) 
  145.     ext = true 
  146.     Dim temp_ext, e 
  147.     temp_ext = Split(ZipFileExt, ";"
  148.     For e = 0 To UBound(temp_ext) 
  149.         If Mid(filename, InstrRev(filename, ".") + 1) = temp_ext(e) Then ext = false 
  150.     Next 
  151. End Function 
  152. %> 
  153. </body>  
  154. </html> 


解压代码:
 

  1. <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%> 
  2. <% Option Explicit %> 
  3. <% On Error Resume Next %> 
  4. <% Response.Charset="UTF-8" %> 
  5. <% Server.ScriptTimeout=99999999 %> 
  6. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  7. <html xmlns="http://www.w3.org/1999/xhtml"
  8. <head> 
  9. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  10. <title>文件解包程序</title> 
  11. </head> 
  12. <body> 
  13. <% 
  14. Dim strLocalPath,strXmlFile 
  15. '要解压的打包成XML的文件 
  16.  
  17. strXmlFile = "update.xml" 
  18. '得到当前文件夹的物理路径 
  19.  
  20. strLocalPath = Left(Request.ServerVariables("PATH_TRANSLATED"), InStrRev(Request.ServerVariables("PATH_TRANSLATED"), "\")) 
  21. Dim objXmlFile 
  22. Dim objNodeList 
  23. Dim objFSO 
  24. Dim objStream 
  25. Dim i, j 
  26. Set objXmlFile = Server.CreateObject("Microsoft.XMLDOM"
  27. objXmlFile.load(Server.MapPath(strXmlFile)) 
  28. If objXmlFile.readyState = 4 Then 
  29.     If objXmlFile.parseError.errorCode = 0 Then 
  30.         Set objNodeList = objXmlFile.documentElement.selectNodes("//folder/path"
  31.         Set objFSO = CreateObject("Scripting.FileSystemObject"
  32.         j = objNodeList.Length -1 
  33.         For i = 0 To j 
  34.             If objFSO.FolderExists(strLocalPath & objNodeList(i).text) = False Then 
  35.                 objFSO.CreateFolder(strLocalPath & objNodeList(i).text) 
  36.             End If 
  37.             Response.Write "创建目录" & objNodeList(i).text & "<br/>" 
  38.             Response.Flush 
  39.         Next 
  40.         Set objFSO = Nothing 
  41.         Set objNodeList = Nothing 
  42.         Set objNodeList = objXmlFile.documentElement.selectNodes("//file/path"
  43.         j = objNodeList.Length -1 
  44.         For i = 0 To j 
  45.             Set objStream = CreateObject("ADODB.Stream"
  46.             With objStream 
  47.                 .Type = 1 
  48.                 .Open 
  49.                 .Write objNodeList(i).nextSibling.nodeTypedvalue 
  50.                 .SaveToFile strLocalPath & objNodeList(i).text, 2 
  51.                 Response.Write "释放文件" & objNodeList(i).text & "<br/>" 
  52.                 Response.Flush 
  53.                 .Close 
  54.             End With 
  55.             Set objStream = Nothing 
  56.         Next 
  57.         Set objNodeList = Nothing 
  58.     End If 
  59. End If 
  60. Set objXmlFile = Nothing 
  61. Response.Write "文件解包完毕" 
  62. %> 
  63. </body> 
  64. </html> 


关于我们-广告合作-联系我们-积分规则-网站地图

Copyright(C)2013-2017版权所属asp编程网