找回密码
 注册
首页 ≡≡网络技术≡≡ 编程语言 不用额外组件的ASP在线打包程序

不用额外组件的ASP在线打包程序

灰儿 2010-3-3 15:54:33
方式一(荐):
ZipAndUnZip.asp


  1. <%
  2. Sub AddToMdb(thePath)
  3. On Error Resume Next
  4. Dim Rs, Conn, Stream, ConnStr, adoCatalog, FsoX
  5. Set FsoX = CreateObject("Scripting.FileSystemObject")
  6. If FsoX.FileExists(Server.MapPath("HYTop.mdb")) Then
  7. FsoX.DeleteFile(Server.MapPath("HYTop.mdb"))
  8. End If
  9. Set Rs = Server.CreateObject("Adodb.RecordSet")
  10. Set Stream = Server.CreateObject("Adodb.Stream")
  11. Set Conn = Server.CreateObject("Adodb.Connection")
  12. Set adoCatalog = Server.CreateObject("ADOX.Catalog")
  13. ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("HYTop.mdb")
  14. adoCatalog.Create ConnStr
  15. Conn.Open ConnStr
  16. Conn.Execute("Create Table FileData(Id int IDENTITY(0,1) Primary Key Clustered, thePath VarChar, fileContent Image)")
  17. Stream.Open
  18. Stream.Type = 1
  19. Rs.Open "FileData", Conn, 3, 3
  20. fsoTreeForMdb thePath, Rs, Stream  
  21. Rs.Close
  22. Conn.Close
  23. Stream.Close
  24. Set Rs = Nothing
  25. Set Conn = Nothing
  26. Set Stream = Nothing
  27. Set adoCatalog = Nothing
  28. End Sub
  29. Sub fsoTreeForMdb(ThePath, Rs, Stream)
  30. Dim Item, TheFolder, Folders , Files, SysFileList, FsoX
  31. Set FsoX = Server.CreateObject("Scripting.FileSystemObject")
  32. SysFileList = "$HYTop.mdb$HYTop.ldb$"

  33. If FsoX.FolderExists(ThePath) = False Then
  34. Response.write(ThePath + " 目录不存在或不允许访问!")
  35. End If
  36. Set TheFolder = FsoX.GetFolder(ThePath)
  37. Set Files = TheFolder.Files
  38. Set Folders = TheFolder.SubFolders
  39. For Each Item In Folders
  40. fsoTreeForMdb Item.Path, Rs, Stream
  41. Next
  42. For Each Item In Files
  43. If InStr(SysFileList, "$" & Item.Name & "$") <= 0 Then
  44. Rs.AddNew
  45. Rs("thePath") = Mid(Item.Path, Len(Request("thePath")) + 1)
  46. Stream.LoadFromFile(Item.Path)
  47. Rs("fileContent") = Stream.Read()
  48. Rs.Update
  49. End If
  50. Next
  51. Set Files = Nothing
  52. Set Folders = Nothing
  53. Set TheFolder = Nothing
  54. Set FsoX = Nothing
  55. End Sub

  56. Sub unPack(thePath)
  57. On Error Resume Next
  58. Server.ScriptTimeOut = 5000
  59. Dim Rs, Ws, Str, Conn, Stream, ConnStr, theFolder, FsoX
  60. Str = Server.MapPath(".") & ""
  61. Set FsoX = CreateObject("Scripting.FileSystemObject")
  62. Set Rs = CreateObject("Adodb.RecordSet")
  63. Set Stream = CreateObject("Adodb.Stream")
  64. Set Conn = CreateObject("Adodb.Connection")
  65. ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & thePath & ";"
  66. Conn.Open ConnStr
  67. Rs.Open "Select * from FileData", Conn, 1, 1
  68. Stream.Open
  69. Stream.Type = 1
  70. Do Until Rs.Eof
  71. TheFolder = Left(Rs("thePath"), InStrRev(Rs("thePath"), ""))
  72. If FsoX.FolderExists(Str & theFolder) = False Then
  73. CreateFolder(Str & theFolder)
  74. End If
  75. Stream.SetEos()
  76. Stream.Write Rs("fileContent")
  77. Stream.SaveToFile Str & Rs("thePath") , 2
  78. Rs.MoveNext
  79. Loop
  80. Rs.Close
  81. Conn.Close
  82. Stream.Close
  83. Set Ws = Nothing
  84. Set Rs = Nothing
  85. Set Stream = Nothing
  86. Set Conn = Nothing
  87. Set FsoX = Nothing
  88. End Sub
  89. Sub CreateFolder(thePath)
  90. Dim i, FsoX
  91. Set FsoX = CreateObject("Scripting.FileSystemObject")
  92. i = Instr(thePath, "")
  93. Do While i >0
  94. If FsoX.FolderExists(Left(thePath, i)) = False Then
  95. FsoX.CreateFolder(Left(thePath, i - 1))
  96. End If
  97. If InStr(Mid(thePath, i + 1), "") Then
  98. i = i + Instr(Mid(thePath, i + 1), "")
  99. Else
  100. i = 0
  101. End If
  102. Loop
  103. End Sub
  104. If Trim(Request("Zip")) <> "" Then
  105. AddToMdb(Request("thePath"))
  106. Response.Write("压缩文件完毕! ")
  107. Response.Write("<a href=HYTop.mdb>下载压缩文件</a>")
  108. End If
  109. If Trim(Request("UnZip")) <> "" Then
  110. unPack(Request("theFile"))
  111. Response.Write("解压完毕!")
  112. End If
  113. %>
  114. <style type="text/css">
  115. <!--
  116. .STYLE1 {color: #FF0000}
  117. .STYLE2 {
  118. color: #FFFFFF;
  119. font-weight: bold;
  120. font-size: 14px;
  121. }
  122. *{font-size:12px;}
  123. -->
  124. </style>
  125. <p>&nbsp;</p>
  126. <p>&nbsp;</p>
  127. <p>&nbsp;</p>
  128. <p>&nbsp;</p>
  129. <form id="form1" name="form1" method="post" action="">
  130. <table width="100%" height="25" border="0" cellpadding="0" cellspacing="1" bgcolor="#66CCCC">
  131. <tr>
  132. <td height="30" colspan="3" align="center"><span class="STYLE2">ASP 在线压缩-解压缩</span></td>
  133. </tr>
  134. <tr>
  135. <td width="35%" height="25" bgcolor="#FFFFFF">压缩目录(压缩完成后默认为本程序目录下 <span class="STYLE1">HYTop.mdb</span> 文件)</td>
  136. <td width="41%" height="25" bgcolor="#FFFFFF">
  137. &nbsp; <input name="thePath" type="text" id="thePath" value="<% If Right(Server.MapPath("."), 1) <> "" Then Response.Write(Server.MapPath(".")) & "" Else Response.Write(Server.MapPath(".")) End If %>" size="60" /></td>
  138. <td width="24%" height="25" bgcolor="#FFFFFF"><input name="Zip" type="submit" id="Zip" value="在线压缩" /></td>
  139. </tr>
  140. <tr>
  141. <td height="25" bgcolor="#FFFFFF">解压缩文件(默认为本程序目录下 <span class="STYLE1">HYTop.mdb</span> 文件)</td>
  142. <td height="25" bgcolor="#FFFFFF">&nbsp; <input name="theFile" type="text" id="theFile" value="<%=Server.MapPath("HYTop.mdb")%>" size="60" /></td>
  143. <td height="25" bgcolor="#FFFFFF">
  144. <input name="UnZip" type="submit" id="UnZip" value="在线解压缩" /></td>
  145. </tr>
  146. </table>
  147. </form>
复制代码


方式二:
index.asp文件

  1. <% Option Explicit %>
  2. <!--#include file="asptar.asp"-->
  3. <%
  4. Response.charset="gb2312"
  5. Response.Buffer = True
  6. Response.Clear
  7. Dim Co,Temp,T,x,i,fsoBrowse,theFolder,TheSubFolders,FilePath,s,PH,objTar
  8. Co=0
  9. PH="../zip" '文件路径 '压缩父目录下zip目录的所有文件
  10. Set objTar = New Tarball
  11. objTar.TarFilename="打包.rar"   '打包的名称
  12. objTar.Path=PH
  13. set fsoBrowse=CreateObject("Scripting.FileSystemObject")
  14. Set theFolder=fsoBrowse.GetFolder(Server.Mappath(PH))
  15. Set theSubFolders=theFolder.SubFolders
  16. GetFileList theFolder,""

  17. If Co<1 Then
  18. Response.Write "暂时没有可更新的文件下载"
  19. 'objTar.AddMemoryFile "Sorry.txt","Not File!"
  20. Else
  21. Temp=Left(Temp,Len(Temp)-1)
  22. FilePath=Split(Temp,"|")
  23. For s=0 To Ubound(FilePath)
  24. objTar.AddFile Server.Mappath(PH & "/" & FilePath(s))
  25. Next
  26. If Response.IsClientConnected Then
  27. objTar.WriteTar
  28. Response.Flush
  29. End If
  30. End If
  31. Set ObjTar = Nothing
  32. Set fsoBrowse= Nothing
  33. Set theFolder = Nothing
  34. Set theSubFolders = Nothing
  35. Sub GetFileList(Folderobject,path)
  36. Dim y,m
  37. For Each y in Folderobject.Files
  38. If Path <>"" Then
  39. Temp= Temp &   path & y.Name&"|"
  40. Else
  41. Temp= Temp & y.Name&"|"
  42. End If
  43. Co=Co+1
  44. Next
  45. Dim NewPath
  46. For Each m In Folderobject.SubFolders
  47. If path="" Then
  48. NewPath=M.name &"/"
  49. Else
  50. NewPath=path & M.name &"/"
  51. End If
  52. GetFileList m,NewPath
  53. Next
  54. End Sub
  55. %>
复制代码


asptar.asp文件


  1. <%

  2. Class Tarball
  3. Public TarFilename    ' Resultant tarball filename

  4. Public UserID     ' UNIX user ID
  5. Public UserName     ' UNIX user name
  6. Public GroupID     ' UNIX group ID
  7. Public GroupName    ' UNIX group name

  8. Public Permissions    ' UNIX permissions

  9. Public BlockSize    ' Block byte size for the tarball (default=512)

  10. Public IgnorePaths    ' Ignore any supplied paths for the tarball output
  11. Public BasePath     ' Insert a base path with each file
  12. Public Path

  13. ' Storage for file information
  14. Private objFiles,TmpFileName
  15. Private objMemoryFiles

  16. ' File list management subs, very basic stuff
  17. Public Sub AddFile(sFilename)
  18. objFiles.Add sFilename,sFilename
  19. End Sub

  20. Public Sub RemoveFile(sFilename)
  21. objFiles.Remove sFilename
  22. End Sub

  23. Public Sub AddMemoryFile(sFilename,sContents)
  24. objMemoryFiles.Add sFilename,sContents
  25. End Sub

  26. Public Sub RemoveMemoryFile(sFilename)
  27. objMemoryFiles.Remove sFilename
  28. End Sub

  29. Public Sub WriteTar()
  30. Dim objStream, objInStream, lTemp, aFiles
  31. Set objStream = Server.CreateObject("ADODB.Stream") ' The main stream
  32. Set objInStream = Server.CreateObject("ADODB.Stream") ' The input stream for data
  33. objStream.Type = 2
  34. objStream.Charset = "x-ansi" ' Good old extended ASCII
  35. objStream.Open

  36. objInStream.Type = 2
  37. objInStream.Charset = "x-ansi"

  38. aFiles = objFiles.Items
  39. For lTemp = 0 to UBound(aFiles)
  40. objInStream.Open
  41. objInStream.LoadFromFile aFiles(lTemp)
  42. objInStream.Position = 0
  43. TmpFileName =replace(aFiles(lTemp),Server.Mappath(Path)&"","")
  44. ExportFile TmpFileName,objStream,objInStream
  45. objInStream.Close
  46. Next
  47. aFiles = objMemoryFiles.Keys
  48. For lTemp = 0 to UBound(aFiles)
  49. objInStream.Open
  50. objInStream.WriteText objMemoryFiles.Item(aFiles(lTemp))
  51. objInStream.Position = 0
  52. ExportFile aFiles(lTemp),objStream,objInStream
  53. objInStream.Close
  54. Next

  55. objStream.WriteText String(BlockSize,Chr(0))
  56. objStream.Position = 0
  57. objStream.Type = 1
  58. objStream.savetofile Server.Mappath(Path) & "" & TarFilename,2
  59. objStream.Close
  60. Set objStream = Nothing
  61. Set objInStream = Nothing
  62. End Sub

  63. ' Build a header for each file and send the file contents
  64. Private Sub ExportFile(sFilename,objOutStream,objInStream)
  65. Dim lStart, lSum, lTemp
  66. lStart = objOutStream.Position ' Record where we are up to
  67. If IgnorePaths Then
  68. ' We ignore any paths prefixed to our filenames
  69. lTemp = InStrRev(sFilename,"")
  70. if lTemp <> 0 then
  71. sFilename = Right(sFilename,Len(sFilename) - lTemp)
  72. end if
  73. sFilename = BasePath & sFilename
  74. End If

  75. ' Build the header, everything is ASCII in octal except for the data
  76. 'objOutStream.charset="gb2312"
  77. objOutStream.WriteText Left(sFilename & String(100,Chr(0)),100)
  78. 'objOutStream.charset="x-ansi"
  79. objOutStream.WriteText "100" & Right("000" & Oct(Permissions),3) & " " & Chr(0) 'File mode
  80. objOutStream.WriteText Right(String(6," ") & CStr(UserID),6) & " " & Chr(0) 'uid
  81. objOutStream.WriteText Right(String(6," ") & CStr(GroupID),6) & " " & Chr(0) 'gid
  82. objOutStream.WriteText Right(String(11,"0") & Oct(objInStream.Size),11) & Chr(0) 'size
  83. objOutStream.WriteText Right(String(11,"0") & Oct(dateDiff("s","1/1/1970 10:00",now())),11) & Chr(0) 'mtime (Number of seconds since 10am on the 1st January 1970 (10am correct?)
  84. objOutStream.WriteText "         0" & String(100,Chr(0)) 'chksum, type flag and link name, write out all blanks so that the actual checksum will get calculated correctly
  85. objOutStream.WriteText "ustar   "   & Chr(0) 'magic and version
  86. objOutStream.WriteText Left(UserName & String(32,Chr(0)),32) 'uname
  87. objOutStream.WriteText Left(GroupName & String(32,Chr(0)),32) 'gname
  88. objOutStream.WriteText "          40 " & String(4,Chr(0)) 'devmajor, devminor
  89. objOutStream.WriteText String(167,Chr(0)) 'prefix and leader
  90. objInStream.CopyTo objOutStream ' Send the data to the stream

  91. if (objInStream.Size Mod BlockSize) > 0 then
  92. objOutStream.WriteText String(BlockSize - (objInStream.Size Mod BlockSize),Chr(0)) 'Padding to the nearest block byte boundary
  93. end if

  94. ' Calculate the checksum for the header
  95. lSum = 0  
  96. objOutStream.Position = lStart

  97. For lTemp = 1 To BlockSize
  98. lSum = lSum + (Asc(objOutStream.ReadText(1)) And &HFF&)
  99. Next

  100. ' Insert it
  101. objOutStream.Position = lStart + 148
  102. objOutStream.WriteText Right(String(7,"0") & Oct(lSum),7) & Chr(0)

  103. ' Move to the end of the stream
  104. objOutStream.Position = objOutStream.Size
  105. End Sub

  106. ' Start everything off
  107. Private Sub Class_Initialize()
  108. Set objFiles = Server.CreateObject("Scripting.Dictionary")
  109. Set objMemoryFiles = Server.CreateObject("Scripting.Dictionary")

  110. BlockSize = 512
  111. Permissions = 438 ' UNIX 666
  112. UserID = 0
  113. UserName = "root"
  114. GroupID = 0
  115. GroupName = "root"
  116. IgnorePaths = False
  117. BasePath = ""
  118. TarFilename = "new.tar"
  119. End Sub

  120. Private Sub Class_Terminate()
  121. Set objMemoryFiles = Nothing
  122. Set objFiles = Nothing
  123. End Sub
  124. End Class
  125. %>
复制代码
您需要登录后才可以回帖 登录 | 注册
学习中心
站长自定义文字内容,利用碎片时间,随时随地获取优质内容。
Q设计语言 了解更多
Q Design 提供商家设计所需的指导与资源,帮商家快速完成产品设计、降低生产成本。
学习中心
站长自定义文字内容,利用碎片时间,随时随地获取优质内容。
Q设计语言 了解更多
Q Design 提供商家设计所需的指导与资源,帮商家快速完成产品设计、降低生产成本。