<!--#include file="UserManageClass.asp" -->
<%
Dim Co,Temp,T,x,i,fsoBrowse,theFolder,TheSubFolders,FilePath,s,PH,objTar
Co=0
   PH=CblogObj.GetUpLoadDir(Session("UserID"))
   Set objTar = New Tarball
   objTar.TarFilename="MyBlogFile.rar"
   objTar.Path=PH
   set fsoBrowse=CreateObject("Scripting.FileSystemObject")
   Set theFolder=fsoBrowse.GetFolder(Server.Mappath(PH))
   Set theSubFolders=theFolder.SubFolders
   For Each T in theFolder.Files
      Temp= Temp & T.Name & "|"
      Co=Co+1
   Next
   For Each x In theSubFolders
      For Each i In X.Files
         Temp= Temp &  X.Name&"/"&i.Name&"|"
         Co=Co+1
      Next
   Next
   If Co<1 Then
      Response.Write "暂时没有可更新的文件下载"
   Else
      Temp=Left(Temp,Len(Temp)-1)
      FilePath=Split(Temp,"|")
      For s=0 To Ubound(FilePath)
        objTar.AddFile Server.Mappath(PH&"/"&FilePath(s))
      Next
   If Response.IsClientConnected Then
        objTar.WriteTar
        Response.Flush
   End If
   End If
   Set ObjTar = Nothing
   Set fsoBrowse= Nothing
   Set theFolder = Nothing
   Set theSubFolders = Nothing
Class Tarball
Public TarFilename

Public UserID
Public UserName
Public GroupID
Public GroupName

Public Permissions

Public BlockSize

Public IgnorePaths
Public BasePath
Public Path
Private objFiles,TmpFileName
Private objMemoryFiles
Public Sub AddFile(sFilename)
  objFiles.Add sFilename,sFilename
End Sub

Public Sub RemoveFile(sFilename)
  objFiles.Remove sFilename
End Sub

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

Public Sub RemoveMemoryFile(sFilename)
  objMemoryFiles.Remove sFilename
End Sub
Public Sub WriteTar()
  Dim objStream, objInStream, lTemp, aFiles

  Set objStream = Server.CreateObject("ADODB.Stream")
  Set objInStream = Server.CreateObject("ADODB.Stream")
  
  objStream.Type = 2
  objStream.Charset = "x-ansi"
  objStream.Open

  objInStream.Type = 2
  objInStream.Charset = "x-ansi"
  aFiles = objFiles.Items
  For lTemp = 0 to UBound(aFiles)
   objInStream.Open
   objInStream.LoadFromFile aFiles(lTemp)
   objInStream.Position = 0
             TmpFileName =replace(aFiles(lTemp),Server.Mappath(Path)&"\","")
    ExportFile TmpFileName,objStream,objInStream
   objInStream.Close
  Next
  aFiles = objMemoryFiles.Keys
  For lTemp = 0 to UBound(aFiles)
   objInStream.Open
   objInStream.WriteText objMemoryFiles.Item(aFiles(lTemp))
   objInStream.Position = 0
   ExportFile aFiles(lTemp),objStream,objInStream
   objInStream.Close
  Next

  objStream.WriteText String(BlockSize,Chr(0))
  objStream.Position = 0
  objStream.Type = 1
  Response.AddHeader "Content-Disposition","filename=" & TarFilename
  Response.ContentType = "application/x-tar"
  Response.BinaryWrite objStream.Read
  objStream.Close
  Set objStream = Nothing
  Set objInStream = Nothing
End Sub
Private Sub ExportFile(sFilename,objOutStream,objInStream)
  Dim lStart, lSum, lTemp
  
  lStart = objOutStream.Position
  
  If IgnorePaths Then
   lTemp = InStrRev(sFilename,"\")
   if lTemp <> 0 then
    sFilename = Right(sFilename,Len(sFilename) - lTemp)
   end if
   sFilename = BasePath & sFilename
  End If
  objOutStream.WriteText Left(sFilename & String(100,Chr(0)),100)
  objOutStream.WriteText "100" & Right("000" & Oct(Permissions),3) & " " & Chr(0) 'File mode
  objOutStream.WriteText Right(String(6," ") & CStr(UserID),6) & " " & Chr(0) 'uid
  objOutStream.WriteText Right(String(6," ") & CStr(GroupID),6) & " " & Chr(0) 'gid
  objOutStream.WriteText Right(String(11,"0") & Oct(objInStream.Size),11) & Chr(0) 'size
  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?)
  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
  objOutStream.WriteText "ustar  "  & Chr(0) 'magic and version
  objOutStream.WriteText Left(UserName & String(32,Chr(0)),32) 'uname
  objOutStream.WriteText Left(GroupName & String(32,Chr(0)),32) 'gname
  objOutStream.WriteText "         40 " & String(4,Chr(0)) 'devmajor, devminor
  objOutStream.WriteText String(167,Chr(0)) 'prefix and leader
  objInStream.CopyTo objOutStream ' Send the data to the stream
  
  if (objInStream.Size Mod BlockSize) > 0 then
   objOutStream.WriteText String(BlockSize - (objInStream.Size Mod BlockSize),Chr(0)) 'Padding to the nearest block byte boundary
  end if
  lSum = 0  
  objOutStream.Position = lStart
  
  For lTemp = 1 To BlockSize
   lSum = lSum + (Asc(objOutStream.ReadText(1)) And &HFF&)
  Next
  objOutStream.Position = lStart + 148
  objOutStream.WriteText Right(String(7,"0") & Oct(lSum),7) & Chr(0)
  objOutStream.Position = objOutStream.Size
End Sub

Private Sub Class_Initialize()
  Set objFiles = Server.CreateObject("Scripting.Dictionary")
  Set objMemoryFiles = Server.CreateObject("Scripting.Dictionary")
  
  BlockSize = 512
  Permissions = 438
  
  UserID = 0
  UserName = "root"
  GroupID = 0
  GroupName = "root"
  
  IgnorePaths = False
  BasePath = ""
  TarFilename = "new.tar"
End Sub
Private Sub Class_Terminate()
  Set objMemoryFiles = Nothing
  Set objFiles = Nothing
End Sub
End Class
%>
