草上飞

搜索引擎优化、网站技术、服务器安全、站长工具等相关交流

« 7zip+vbs+windwos计划任务实现定时远程异地服务器备份QQ微薄邀请码发放 »

vbs实现文件分段下载

'为每天建立一个目录

dim fso,fd,f
set fso=createobject("scripting.filesystemobject")
foldername=fso.GetFolder(".").Path&"\"&Date()
'先判定文件夹是否存在
adosCacheSize=100*1024*1024'设为100M,
BlockSize=50*1024*1024'此处表示50M
If Not fso.FolderExists(foldername) Then
 fso.CreateFolder foldername
End if
for i=0 to 9   '从0-9循环。服务器上备份的文件为linkhelperdata.zip.001 linkhelperdata.zip.002等。为0的情况,是如果没有分包的话。9个,总共有9*800M=7.2G的文件大小。应该足够。
 If i=0 Then
  filename="linkhelperdata.zip"
 Else
  filename="linkhelperdata.zip.00"&i
 End If
 downurl="http://www.linkhelper.cn/"&filename
 If fso.FileExists(foldername&"\"&filename) Then'判断要下载的文件是否已经存在
  RangeStart=fso.GetFile(foldername&"\"&filename).Size'若存在,以当前文件大小作为开始位置
 Else
  RangeStart=0'若不存在,一切从零开始
  'fso.CreateTextFile(foldername&"\"&filename).Close'新建文件
 End If
 FileDownStart=RangeStart

 'msgbox lenb(RangeStart)
 Set ados = CreateObject("Adodb.Stream")
 Set adosCache=CreateObject("Adodb.Stream")

 adosCache.Type=1
 adosCache.Mode=3
 adosCache.Open
 Do
 BlockStartTime=Timer()
 Set xPost =createObject("Microsoft.XMLHTTP")
 xPost.Open "GET",downurl,0 ''''下载文件的地址
 xPost.setRequestHeader "Accept","*/*"
 xPost.setRequestHeader "Range","bytes="&RangeStart&"-"&Cstr(RangeStart+BlockSize-1)'
 'MsgBox "bytes="&RangeStart&"-"&Cstr(RangeStart+BlockSize-1)
 xPost.setRequestHeader "Content-Type","application/octet-stream"
 xPost.setRequestHeader "Pragma","no-cache"
 xPost.setRequestHeader "Cache-Control","no-cache"
 xPost.Send()
 'MsgBox xPost.status
 If  xPost.status=404 Then
  
  Exit Do
  If i>0 then
   exit For
  End if
 End if
 
 If xPost.status = 416 Then'

  Exit Do
 End If
 If RangeStart=0 Then
  fso.CreateTextFile(foldername&"\"&filename).Close'新建文件
 End if
 If adosCache.Size >= adosCacheSize Then
  ados.type=1
  ados.mode=3
  ados.open
  ados.LoadFromFile foldername&"\"&filename
  ados.position=ados.size
  adosCache.Position = 0
  'msgbox ados.size
  'response.write "adosCache.size:"&adosCache.size&"<br>"
  'response.write "ados.size:"&ados.size&"<br>"
  'response.flush
  'response.end
  ados.Write adosCache.Read
  ados.SaveToFile foldername&"\"&filename,2
  ados.close
  adosCache.Position=0
  adosCache.SetEOS
 End If
 'MsgBox lenb(xPost.responseBody)
 'On Error Resume next
 'response.write "adosCache.size:"&adosCache.size&"<br>"
 'response.write "xPost.responseBody:"&Lenb(xPost.responseBody)&"<br>"
 'response.flush
 adosCache.Write xPost.responseBody'
 'If Err.number<>0 Then
 ' Err.clear
 ' response.write "adosCache.size:"&adosCache.size&"<br>"
 ' response.write "xPost.responseBody:"&Len(xPost.responseBody)&"<br>"
 ' response.write Err.description
 ' response.end
 'End if
 ResponseRange=xPost.getResponseHeader("Content-Range")
 'MsgBox xPost.getAllResponseHeaders()
 temp=Mid(ResponseRange,Instr(ResponseRange,"-")+1)
 CurrentLastBytes=cdbl(Left(temp,Instr(temp,"/")-1))
 TotalBytes = cdbl(Mid(temp,Instr(temp,"/")+1))
 'MsgBox CurrentLastBytes
 'MsgBox TotalBytes
 If TotalBytes - CurrentLastBytes = 1 Then
  FileDownEnd=TotalBytes
  ados.Type=1'
  ados.Mode=3'
  ados.Open
  ados.LoadFromFile foldername&"\"&filename
  ados.position=ados.size
  adosCache.Position = 0
  ados.Write adosCache.Read
  ados.SaveToFile foldername&"\"&filename,2
  ados.close
  Exit do
 End If
 RangeStart=RangeStart+BlockSize
 loop
 adosCache.close
 Set adosCache=Nothing

 Set ados=nothing
 set xPost=nothing
next
'删除history


 

  • 相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

Powered By Z-Blog 1.8 Arwen Build 90619

© 2008 Designed By 草上飞 QQ:1469886   沪ICP备08014007号   Powered By Z-Blog