Nmlxly 发表于 2009-09-28 20:31:16 有751 人看过

其实实现的思路也很简单,主要就是利用的是xml文件可以存放二进制数据的原理。

两个主要文件如下:

  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   
  15. Dim startime,endtime   
  16.  
  17. '方式一:打包指定路径文件夹内文件。在此更改要打包文件夹的路径   
  18. 'ZipPathDir = "D:\Personal\Desktop\TEM_EDIT"  
  19.  
  20. '方式二:打包当前文件夹内文件。  
  21. '获取当前所在文件夹  
  22. ZipPathDir=Left(Request.ServerVariables("PATH_TRANSLATED"),InStrRev(Request.ServerVariables("PATH_TRANSLATED"),"\"))   
  23. ZipPathFile = "update.xml"   
  24. if right(ZipPathDir,1)<>"\" then ZipPathDir=ZipPathDir&"\"   
  25. '开始打包   
  26. CreateXml(ZipPathFile)   
  27.  
  28. '遍历目录内的所有文件以及文件夹   
  29. sub LoadData(DirPath)   
  30.     dim XmlDoc   
  31.     dim fso 'fso对象   
  32.     dim objFolder '文件夹对象   
  33.     dim objSubFolders '子文件夹集合   
  34.     dim objSubFolder '子文件夹对象   
  35.     dim objFiles '文件集合   
  36.     dim objFile '文件对象   
  37.     dim objStream   
  38.     dim pathname,TextStream,pp,Xfolder,Xfpath,Xfile,Xpath,Xstream   
  39.     dim PathNameStr   
  40.     response.Write("<b>打包子文件夹【 "&DirPath&" 】下的文件:</b><br>")   
  41.     set fso=server.CreateObject("scripting.filesystemobject")   
  42.     set objFolder=fso.GetFolder(DirPath)'创建文件夹对象   
  43.     Response.Write DirPath   
  44.     Response.flush   
  45.     Set XmlDoc = Server.CreateObject("Microsoft.XMLDOM")   
  46.     XmlDoc.load Server.MapPath(ZipPathFile)   
  47.     XmlDoc.async=false   
  48.     '写入每个文件夹路径   
  49.     set Xfolder = XmlDoc.SelectSingleNode("//root").AppendChild(XmlDoc.CreateElement("folder"))   
  50.     Set Xfpath = Xfolder.AppendChild(XmlDoc.CreateElement("path"))   
  51.     Xfpath.text = replace(DirPath,ZipPathDir,"")   
  52.     set objFiles=objFolder.Files   
  53.     for each objFile in objFiles   
  54.         if lcase(DirPath & objFile.name) <> lcase(Request.ServerVariables("PATH_TRANSLATED")) then   
  55.             Response.Write " ...<br/>"   
  56.             PathNameStr = DirPath & "" & objFile.name   
  57.             Response.Write PathNameStr & ""   
  58.             Response.flush   
  59.             '================================================   
  60.             '写入文件的路径及文件内容   
  61.             set Xfile = XmlDoc.SelectSingleNode("//root").AppendChild(XmlDoc.CreateElement("file"))   
  62.             Set Xpath = Xfile.AppendChild(XmlDoc.CreateElement("path"))   
  63.             Xpath.text = replace(PathNameStr,ZipPathDir,"")   
  64.             '创建文件流读入文件内容,并写入XML文件中   
  65.             Set objStream = Server.CreateObject("ADODB.Stream")   
  66.             objStream.Type = 1   
  67.             objStream.Open()   
  68.             objStream.LoadFromFile(PathNameStr)   
  69.             objStream.position = 0   
  70.             Set Xstream = Xfile.AppendChild(XmlDoc.CreateElement("stream"))   
  71.             Xstream.SetAttribute "xmlns:dt","urn:schemas-microsoft-com:datatypes"   
  72.             '文件内容采用二制方式存放   
  73.             Xstream.dataType = "bin.base64"   
  74.             Xstream.nodeTypedValue = objStream.Read()   
  75.             set objStream=nothing   
  76.             set Xpath = nothing   
  77.             set Xstream = nothing   
  78.             set Xfile = nothing   
  79.             '================================================   
  80.         end if   
  81.     next   
  82.     Response.Write "<p>"   
  83.     XmlDoc.Save(Server.Mappath(ZipPathFile))   
  84.     set Xfpath = nothing   
  85.     set Xfolder = nothing   
  86.     set XmlDoc = nothing   
  87.     '创建的子文件夹对象   
  88.     set objSubFolders=objFolder.Subfolders   
  89.     '调用递归遍历子文件夹   
  90.     for each objSubFolder in objSubFolders   
  91.         pathname = DirPath & objSubFolder.name & "\"   
  92.         LoadData(pathname)   
  93.     next   
  94.     set objFolder=nothing   
  95.     set objSubFolders=nothing   
  96.     set fso=nothing   
  97. end sub   
  98.  
  99. '创建一个空的XML文件,为写入文件作准备   
  100. sub CreateXml(FilePath)   
  101.     '程序开始执行时间   
  102.     startime=timer()   
  103.     dim XmlDoc,Root   
  104.     Set XmlDoc = Server.CreateObject("Microsoft.XMLDOM")   
  105.     XmlDoc.async = False   
  106.     Set Root = XmlDoc.createProcessingInstruction("xml","version='1.0' encoding='UTF-8'")   
  107.     XmlDoc.appendChild(Root)   
  108.     XmlDoc.appendChild(XmlDoc.CreateElement("root"))   
  109.     XmlDoc.Save(Server.MapPath(FilePath))   
  110.     Set Root = Nothing   
  111.     Set XmlDoc = Nothing   
  112.     LoadData(ZipPathDir)   
  113.     '程序结束时间   
  114.     endtime=timer()   
  115.     response.write "<br />所有文件解包完毕!<br /><br />"   
  116.     response.Write("页面执行时间:" & FormatNumber((endtime-startime),3) & "秒")   
  117. end sub   
  118. %>   
  119. </body>   
  120. </html> 
通告:http://www.nmlxly.com/Article/44/Trackback.ashx
七嘴八舌信息订阅:http://www.nmlxly.com/Article/44/Feeds.ashx
    暂时没有评论
(必填)
(必填,不会被公开)