10017
+ − 1
' w32DownloadUnzip.vbs
+ − 2
' Download a zipfile and uncompress it with no external tools in Windows
+ − 3
'
+ − 4
' Copyright (c) 2012, Vittorio Giovara, <vittorio.giovara@gmail.com>
+ − 5
' Redistribution and use is allowed according to the terms of the BSD license.
+ − 6
'
+ − 7
' References
+ − 8
' http://superuser.com/questions/59465/is-it-possible-to-download-using-the-windows-command-line
+ − 9
' http://stackoverflow.com/questions/1021557/how-to-unzip-a-file-using-the-command-line
+ − 10
' http://stackoverflow.com/questions/424331/get-the-current-temporary-directory-path-in-vbscript
+ − 11
+ − 12
Set ArgObj = WScript.Arguments
+ − 13
+ − 14
If (Wscript.Arguments.Count = 1) Then
+ − 15
strFileURL = ArgObj(0)
+ − 16
strOutputPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
+ − 17
Else
+ − 18
If (Wscript.Arguments.Count = 2) Then
+ − 19
strFileURL = ArgObj(0)
+ − 20
strOutputPath = ArgObj(1)
+ − 21
Else
+ − 22
WScript.Echo ("Usage: csript.exe w32DownloadUnzip.vbs url output")
+ − 23
WScript.Quit
+ − 24
End if
+ − 25
End if
+ − 26
+ − 27
' Temporary directory
+ − 28
strHDLocation = WScript.CreateObject("Scripting.FileSystemObject").GetSpecialFolder(2) + "\hwlibtemp.zip"
+ − 29
+ − 30
' Fetch the file
+ − 31
WScript.Echo ( "Trying to download from " & strFileURL)
+ − 32
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
+ − 33
objXMLHTTP.open "GET", strFileURL, false
+ − 34
objXMLHTTP.send()
+ − 35
+ − 36
If objXMLHTTP.Status = 200 Then
+ − 37
Set objADOStream = CreateObject("ADODB.Stream")
+ − 38
objADOStream.Open
+ − 39
objADOStream.Type = 1 'adTypeBinary
+ − 40
+ − 41
objADOStream.Write objXMLHTTP.ResponseBody
+ − 42
objADOStream.Position = 0 'Set the stream position to the start
+ − 43
+ − 44
Set objFSO = Createobject("Scripting.FileSystemObject")
+ − 45
If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
+ − 46
Set objFSO = Nothing
+ − 47
+ − 48
objADOStream.SaveToFile strHDLocation
+ − 49
objADOStream.Close
+ − 50
Set objADOStream = Nothing
+ − 51
Set objXMLHTTP = Nothing
+ − 52
Else
+ − 53
WScript.Echo ("Error downloading file (error code: " & objXMLHTTP.Status & ")")
+ − 54
Set objXMLHTTP = Nothing
+ − 55
WScript.Quit
+ − 56
End if
+ − 57
+ − 58
WScript.Echo ( "Extracting file to " & strOutputPath)
+ − 59
Set objShell = CreateObject( "Shell.Application" )
+ − 60
Set objSource = objShell.NameSpace(strHDLocation).Items()
+ − 61
Set objTarget = objShell.NameSpace(strOutputPath)
+ − 62
intOptions = 16 'no user prompt
+ − 63
objTarget.CopyHere objSource, intOptions
+ − 64
+ − 65
WScript.Echo ( "Success!" )
+ − 66
Set objShell = Nothing