本帖最後由 rictirse 於 2014-12-24 08:26 編輯
基於上一版大家給的建議,這邊噹噹稍作修改
可以比對MD5,新版本辨識更加準確
順便撰寫了一個檔案產生器,方便大量傳檔
下載器內部的子控數量會隨著檔案數量而變化
如果你檔案在aspx內只有一筆那在更新器也只會看到一筆資料
防止錯誤下載
這邊有使用教學影片歡迎跟噹噹來來研究
自動更新辨識器- #include <IE.au3>
- #include <Array.au3>
- #include <GUIConstantsEx.au3>
- Dim $DirPath = @ScriptDir&"Download"
- $oIE = _IECreate( "http://myweb.hinet.net/home19/leojackey/Update.aspx", 1, 0, 1)
- $sText = _IEBodyReadText($oIE)
- _IEQuit ($oIE)
- $Line = StringSplit ( $sText, @LF, 2)
- Global $Array[UBound($Line)][3]
- Global $Local_MD5[UBound($Line)]
- Global $Download[UBound($Line)]
- For $i = 0 To (UBound($Line)-1)
- $Split = StringSplit ( $Line[$i], @TAB, 2)
- $Array[$i][0] = $Split[0]
- $Array[$i][1] = $Split[1]
- $Array[$i][2] = $Split[2]
- Next
- GUICreate ( "Auto Update", 710, 40+($i*40))
- GUICtrlCreateLabel ( "更新進度", 10, 13)
- $Progres = GUICtrlCreateProgress ( 70, 10, 628, 20)
- For $i = 0 To (UBound($Line)-1)
- GUICtrlCreateLabel ( "軟體名稱:"&$Array[$i][1], 10, 37+($i*40), 290, 40)
- GUICtrlSetFont( -1, 18, 800)
- GUICtrlCreateLabel ( "Server MD5:"&$Array[$i][2], 310, 40+($i*40), 300)
- $Local_MD5[$i] = GUICtrlCreateLabel ( "Local MD5:"&$Array[$i][2], 310, 55+($i*40), 300)
- $Download[$i] = GUICtrlCreateButton ( "下載", 650, 42+($i*40), 50, 25)
- GUICtrlSetState ( -1, $GUI_DISABLE)
- Next
- _CheckMD5()
- GUISetState()
- While True
- $msg = GUIGetMsg()
- For $i = 0 To (UBound($Line)-1)
- If $msg = $Download[$i] Then
- _Download ( $Array[$i][0], $DirPath&""&$Array[$i][1])
- GUICtrlSetState ( $Download[$i], $GUI_DISABLE)
- EndIf
- Next
- If $msg = $GUI_EVENT_CLOSE Then Exit
- WEnd
- Func _Download ( $FilePath_Server, $FilePath_Local)
- $TotalSize = InetGetSize ($FilePath_Server) ;; 取得總容量
- $hDownload = InetGet ($FilePath_Server, $FilePath_Local, 1, 1) ;;開始下載
- Do
- Sleep (50)
- $NowDownload = InetGetInfo($hDownload, 0)
- $per = Int ($NowDownload/$TotalSize*100) ;;計算百分比
- $res = Int (StringLeft ( $per, 3)) ;;百分值
- GUICtrlSetData ($progres , $res) ;;下載進度
- TrayTip ( "正在下載更新中", "已下載 " & Int ($NowDownload/1024)&" kB", 1) ;;監視已下載大小
- Until InetGetInfo ( $hDownload, 2)
- GuiCtrlSetData ( $progres , 0)
- If $TotalSize = FileGetSize ($FilePath_Local) Then
- ;~ MsgBox ( 0, "更新完成", "檔案大小:" &Int ($TotalSize/1024)&" kB"&@CRLF&"已下載大小:" & Int ($NowDownload/1024)&" kB")
- Run($FilePath_Local) ;;執行程序
- Else
- MsgBox ( 16, "錯誤", "更新失敗。")
- EndIf
- EndFunc
- Func _CheckMD5()
- Local $MD5
- For $i = 0 To (UBound($Line)-1)
- If FileExists ($DirPath&""&$Array[$i][1]) Then
- $MD5 = _MD5($DirPath&""&$Array[$i][1])
- GUICtrlSetData ( $Local_MD5[$i], "Local MD5:"&$MD5)
- If $MD5 = $Array[$i][2] Then GUICtrlSetState ( $Download[$i], $GUI_DISABLE)
- Else
- GUICtrlSetData ( $Local_MD5[$i], "Local MD5:未發現")
- GUICtrlSetState ( $Download[$i], $GUI_ENABLE)
- EndIf
- Next
- EndFunc
- Func _MD5($sFile)
- Local Const $CALG_MD2 = 0x8001
- Local Const $CALG_MD4 = 0x8002
- Local Const $CALG_MD5 = 0x8003
- Local Const $CALG_SHA1 = 0x8004
- $hFile = FileOpen ($sFile, 16)
- $bData = FileRead ($hFile)
- $iLength = BinaryLen ($bData)
- FileClose($hFile)
- $tBuffer = DllStructCreate("byte[" & $iLength & "]")
- $pBuffer = DllStructGetPtr($tBuffer)
- DllStructSetData($tBuffer, 1, $bData)
- $iResult = DllCall ("Crypt32.dll", "int", "CryptHashCertificate", "hWnd", 0, "dword", $CALG_MD5, "dword", 0, "ptr", $pBuffer, "dword", $iLength, "ptr", 0, "dword*", 0)
- $tResult = DllStructCreate ("byte[" & $iResult[7] & "]")
- $pResult = DllStructGetPtr ($tResult)
- $iResult = DllCall ("Crypt32.dll", "int", "CryptHashCertificate", "hWnd", 0, "dword", $CALG_MD5, "dword", 0, "ptr", $pBuffer, "dword", $iLength, "ptr", $pResult, "dword*", $iResult[7])
- If $iResult[0] <> 0 Then Return StringTrimLeft (DllStructGetData ($tResult, 1), 2)
- $tBuffer = 0
- $tResult = 0
- EndFunc
複製代碼 aspx 產生器- #include <IE.au3>
- #include <Array.au3>
- #include <GUIConstantsEx.au3>
- Dim $DirPath = @ScriptDir&"Download"
- $oIE = _IECreate( "http://myweb.hinet.net/home19/leojackey/Update.aspx", 1, 0, 1)
- $sText = _IEBodyReadText($oIE)
- _IEQuit ($oIE)
- $Line = StringSplit ( $sText, @LF, 2)
- Global $Array[UBound($Line)][3]
- Global $Local_MD5[UBound($Line)]
- Global $Download[UBound($Line)]
- For $i = 0 To (UBound($Line)-1)
- $Split = StringSplit ( $Line[$i], @TAB, 2)
- $Array[$i][0] = $Split[0]
- $Array[$i][1] = $Split[1]
- $Array[$i][2] = $Split[2]
- Next
- GUICreate ( "Auto Update", 710, 40+($i*40))
- GUICtrlCreateLabel ( "更新進度", 10, 13)
- $Progres = GUICtrlCreateProgress ( 70, 10, 628, 20)
- For $i = 0 To (UBound($Line)-1)
- GUICtrlCreateLabel ( "軟體名稱:"&$Array[$i][1], 10, 37+($i*40), 290, 40)
- GUICtrlSetFont( -1, 18, 800)
- GUICtrlCreateLabel ( "Server MD5:"&$Array[$i][2], 310, 40+($i*40), 300)
- $Local_MD5[$i] = GUICtrlCreateLabel ( "Local MD5:"&$Array[$i][2], 310, 55+($i*40), 300)
- $Download[$i] = GUICtrlCreateButton ( "下載", 650, 42+($i*40), 50, 25)
- GUICtrlSetState ( -1, $GUI_DISABLE)
- Next
- _CheckMD5()
- GUISetState()
- While True
- $msg = GUIGetMsg()
- For $i = 0 To (UBound($Line)-1)
- If $msg = $Download[$i] Then
- _Download ( $Array[$i][0], $DirPath&""&$Array[$i][1])
- GUICtrlSetState ( $Download[$i], $GUI_DISABLE)
- EndIf
- Next
- If $msg = $GUI_EVENT_CLOSE Then Exit
- WEnd
- Func _Download ( $FilePath_Server, $FilePath_Local)
- $TotalSize = InetGetSize ($FilePath_Server) ;; 取得總容量
- $hDownload = InetGet ($FilePath_Server, $FilePath_Local, 1, 1) ;;開始下載
- Do
- Sleep (50)
- $NowDownload = InetGetInfo($hDownload, 0)
- $per = Int ($NowDownload/$TotalSize*100) ;;計算百分比
- $res = Int (StringLeft ( $per, 3)) ;;百分值
- GUICtrlSetData ($progres , $res) ;;下載進度
- TrayTip ( "正在下載更新中", "已下載 " & Int ($NowDownload/1024)&" kB", 1) ;;監視已下載大小
- Until InetGetInfo ( $hDownload, 2)
- GuiCtrlSetData ( $progres , 0)
- If $TotalSize = FileGetSize ($FilePath_Local) Then
- ;~ MsgBox ( 0, "更新完成", "檔案大小:" &Int ($TotalSize/1024)&" kB"&@CRLF&"已下載大小:" & Int ($NowDownload/1024)&" kB")
- Run($FilePath_Local) ;;執行程序
- Else
- MsgBox ( 16, "錯誤", "更新失敗。")
- EndIf
- EndFunc
- Func _CheckMD5()
- Local $MD5
- For $i = 0 To (UBound($Line)-1)
- If FileExists ($DirPath&""&$Array[$i][1]) Then
- $MD5 = _MD5($DirPath&""&$Array[$i][1])
- GUICtrlSetData ( $Local_MD5[$i], "Local MD5:"&$MD5)
- If $MD5 = $Array[$i][2] Then GUICtrlSetState ( $Download[$i], $GUI_DISABLE)
- Else
- GUICtrlSetData ( $Local_MD5[$i], "Local MD5:未發現")
- GUICtrlSetState ( $Download[$i], $GUI_ENABLE)
- EndIf
- Next
- EndFunc
- Func _MD5($sFile)
- Local Const $CALG_MD2 = 0x8001
- Local Const $CALG_MD4 = 0x8002
- Local Const $CALG_MD5 = 0x8003
- Local Const $CALG_SHA1 = 0x8004
- $hFile = FileOpen ($sFile, 16)
- $bData = FileRead ($hFile)
- $iLength = BinaryLen ($bData)
- FileClose($hFile)
- $tBuffer = DllStructCreate("byte[" & $iLength & "]")
- $pBuffer = DllStructGetPtr($tBuffer)
- DllStructSetData($tBuffer, 1, $bData)
- $iResult = DllCall ("Crypt32.dll", "int", "CryptHashCertificate", "hWnd", 0, "dword", $CALG_MD5, "dword", 0, "ptr", $pBuffer, "dword", $iLength, "ptr", 0, "dword*", 0)
- $tResult = DllStructCreate ("byte[" & $iResult[7] & "]")
- $pResult = DllStructGetPtr ($tResult)
- $iResult = DllCall ("Crypt32.dll", "int", "CryptHashCertificate", "hWnd", 0, "dword", $CALG_MD5, "dword", 0, "ptr", $pBuffer, "dword", $iLength, "ptr", $pResult, "dword*", $iResult[7])
- If $iResult[0] <> 0 Then Return StringTrimLeft (DllStructGetData ($tResult, 1), 2)
- $tBuffer = 0
- $tResult = 0
- EndFunc
複製代碼 |