|
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 |
|
11 Set ArgObj = WScript.Arguments |
|
12 |
|
13 If (Wscript.Arguments.Count = 1) Then |
|
14 strFileURL = ArgObj(0) |
|
15 strOutputPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".") |
|
16 Else |
|
17 If (Wscript.Arguments.Count = 2) Then |
|
18 strFileURL = ArgObj(0) |
|
19 strOutputPath = ArgObj(1) |
|
20 Else |
|
21 WScript.Echo ("Usage: csript.exe w32DownloadUnzip.vbs url output") |
|
22 WScript.Quit |
|
23 End if |
|
24 End if |
|
25 |
|
26 strHDLocation = "C:\Windows\Temp\temp.zip" |
|
27 |
|
28 ' Fetch the file |
|
29 Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP") |
|
30 objXMLHTTP.open "GET", strFileURL, false |
|
31 objXMLHTTP.send() |
|
32 |
|
33 If objXMLHTTP.Status = 200 Then |
|
34 WScript.Echo ( "Downloading file from " & strFileURL) |
|
35 Set objADOStream = CreateObject("ADODB.Stream") |
|
36 objADOStream.Open |
|
37 objADOStream.Type = 1 'adTypeBinary |
|
38 |
|
39 objADOStream.Write objXMLHTTP.ResponseBody |
|
40 objADOStream.Position = 0 'Set the stream position to the start |
|
41 |
|
42 Set objFSO = Createobject("Scripting.FileSystemObject") |
|
43 If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation |
|
44 Set objFSO = Nothing |
|
45 |
|
46 objADOStream.SaveToFile strHDLocation |
|
47 objADOStream.Close |
|
48 Set objADOStream = Nothing |
|
49 Else |
|
50 WScript.Echo ("Error downloading file (error code: " & objXMLHTTP.Status & ")") |
|
51 Set objXMLHTTP = Nothing |
|
52 WScript.Quit |
|
53 End if |
|
54 Set objXMLHTTP = Nothing |
|
55 |
|
56 WScript.Echo ( "Extracting file to " & strOutputPath) |
|
57 Set objShell = CreateObject( "Shell.Application" ) |
|
58 Set objSource = objShell.NameSpace(strHDLocation).Items() |
|
59 Set objTarget = objShell.NameSpace(strOutputPath) |
|
60 intOptions = 16 'no user prompt |
|
61 objTarget.CopyHere objSource, intOptions |
|
62 |
|
63 WScript.Echo ( "Extraction successful" ) |
|
64 Set objShell = Nothing |