支持斷點下載的VBS代碼

字號:


    之前我就介紹過VBScript語言的強大。今天再給出一個支持斷點下載的VBS代碼。
    并附上VBS代碼的解析,不懂的朋友可以配合微軟的SCRIPT56.CHM文檔自學。很簡單,
    VBS的好處就是代碼易于理解?;旧厦啃写a執(zhí)行功能都用英文表示出來了。
    這個代碼也是對我以前介紹的VBS下載功能的補充。
    老規(guī)矩,復制保存為dl.vbe。
    不過這個VBS的代碼的不同之處不是雙擊運行,而是在CMD命令行下執(zhí)行。
    下載功能執(zhí)行的格式是: cscript.exe dl.vbs (目標文件地址)
    [以下載MetaSploit的WIn32版本為例。在CMD中輸入:cscript.exe dl.vbs http://spool.metasploit.com/releases/framework-3.2.exe]
    36.7M的文件下載用了7分多鐘,而迅雷用了1分50秒。
    代碼如下:
    if (lcase(right(wscript.fullname,11))="wscript.exe") then'判斷腳本宿主的名稱'
    die("Script host must be CScript.exe.") '腳本宿主不是CScript,于是就die了'
    end if
    if wscript.arguments.count<1 then'至少要有一個參數(shù)'
    die("Usage: cscript webdl.vbs url [filename]") '麻雀雖小五臟俱全,Usage不能忘'
    end if
    url=wscript.arguments(0) '參數(shù)數(shù)組下標從0開始'
    if url="" then die("URL can't be null.") '敢唬我,空url可不行'
    if wscript.arguments.count>1 then'先判斷參數(shù)個數(shù)是否大于1'
    filename=wscript.arguments(1) '再訪問第二個參數(shù)'
    else '如果沒有給出文件名,就從url中獲得'
    t=instrrev(url,"/") '獲得最后一個"/"的位置'
    if t=0 or t=len(url) then die("Can not get filename to save.") '沒有"/"或以"/"結(jié)尾'
    filename=right(url,len(url)-t)'獲得要保存的文件名'
    end if
    if not left(url,7)="http://" then url="http://"&url'如果粗心把“http://”忘了,加上'
    set fso=wscript.createobject("Scripting.FileSystemObject") 'FSO,ASO,HTTP三個對象一個都不能少'
    set aso=wscript.createobject("ADODB.Stream")
    set http=wscript.createobject("Microsoft.XMLHTTP")
    if fso.fileexists(filename) then '判斷要下載的文件是否已經(jīng)存在'
    start=fso.getfile(filename).size '存在,以當前文件大小作為開始位置'
    else
    start=0 '不存在,一切從零開始'
    fso.createtextfile(filename).close '新建文件'
    end if
    wscript.stdout.write "Connectting..." '好戲剛剛開始'
    current=start '當前位置即開始位置'
    do
    http.open "GET",url,true'這里用異步方式調(diào)用HTTP'
    http.setrequestheader "Range","bytes="&start&"-"&cstr(start+20480) '斷點續(xù)傳的奧秘就在這里'
    http.setrequestheader "Content-Type:","application/octet-stream"
    http.send '構(gòu)造完數(shù)據(jù)包就開始發(fā)送'
    for i=1 to 120 '循環(huán)等待'
    if http.readystate=3 then showplan() '狀態(tài)3表示開始接收數(shù)據(jù),顯示進度'
    if http.readystate=4 then exit for '狀態(tài)4表示數(shù)據(jù)接受完成'
    wscript.sleep 500 '等待500ms'
    next
    if not http.readystate=4 then die("Timeout.") '1分鐘還沒下完20k?超時!'
    if http.status>299 then die("Error: "&http.status&" "&http.statustext) '不是吧,又出錯?'
    if not http.status=206 then die("Server Not Support Partial Content.") '服務(wù)器不支持斷點續(xù)傳'
    aso.type=1 '數(shù)據(jù)流類型設(shè)為字節(jié)'
    aso.open
    aso.loadfromfile filename '打開文件'
    aso.position=start'設(shè)置文件指針初始位置'
    aso.write http.responsebody '寫入數(shù)據(jù)'
    aso.savetofile filename,2 '覆蓋保存'
    aso.close
    range=http.getresponseheader("Content-Range") '獲得http頭中的"Content-Range"'
    if range="" then die("Can not get range.")'沒有它就不知道下載完了沒有'
    temp=mid(range,instr(range,"-")+1) 'Content-Range是類似123-456/789的樣子'
    current=clng(left(temp,instr(temp,"/")-1))'123是開始位置,456是結(jié)束位置'
    total=clng(mid(temp,instr(temp,"/")+1)) '789是文件總字節(jié)數(shù)'
    if total-current=1 then exit do '結(jié)束位置比總大小少1就表示傳輸完成了'
    start=start+20480 '否則再下載20k'
    loop while true
    wscript.echo chr(13)&"Download ("&total&") Done." '下載完了,顯示總字節(jié)數(shù)'
    function die(msg) '函數(shù)名來自Perl內(nèi)置函數(shù)die'
    wscript.echo msg '交代遺言^_^'
    wscript.quit '去見馬克思了'
    end function
    function showplan() '顯示下載進度'
    if i mod 3 = 0 then c="/" '簡單的動態(tài)效果'
    if i mod 3 = 1 then c="-"
    if i mod 3 = 2 then c="\"
    wscript.stdout.write chr(13)&"Download ("¤t&") "&c&chr(8)'13號ASCII碼是回到行首,8號是退格'
    end function
    以上就是完整的用VBS寫的支持斷點的下載代碼,非常適合公司禁止用XunLei、Flashget的情況。只是速度是個問題。需要完善到多線程下載。