我有一個vbscript腳本,需要一個.ico文件並創建一個桌面快捷方式使用它作爲它的圖標,但我希望能夠擁有腳本所需的所有內容,如果可能的話。是否可以將.ico文件存儲在vbscript中?
回答
此HTA告訴你如何嵌入一個EXE文件或COM在一個VBScript
對於拉鍊爲例內嵌我的WGET.EXE下載PNG文件= estabanner5.png只是爲了測試
[HTA] Encapsulate a zipped exe file in a VBScript
,我發現這個VBScript的命名基本Base64- Encode- Decode.vbs 淨您剛落,一個文件到腳本的d單擊YES進行編碼。然後單擊「否」解碼Base64字符串。
'-- This is a barebones Base64 encoder/decoder. Drop a file onto script and click YES
'-- to encode. Click NO to decode a Base64 string.
'-- This script uses only VBS and FileSystemObject to do its work. The basic function
' of Base64 conversion is to take each 3 bytes of binary data and convert it to 4
' 6-bit units, which allows any data to be stored as plain text because on plain
' text ASCII characters are used. Decoding is the reverse.
' FSO is designed to only handle text data. Special treatment is required to handle
' binary data, but FSO *can* do it. For example, Textstream.ReadAll expects to read
' a string, so it will return file bytes up until the first null byte. But Textstream.Read(length-of-file)
' can be used to read in the entire file as a string, regardless the content. The bytes can
' then be handled by using Asc to convert the string into a numeric array. It's inefficient,
' but it works. When the file is written back to disk the array members are then converted
' back to characters and the whole thing is transferred as a string. That works fine as
' long as one doesn't try to handle it as a string. For instance, checking Len of the string
' returned from DecodeBase64 will only return the position of the first null.
' The vbCrLf option with encoding is to accomodate email, which by tradition
' inserts a return every 76 characters. In other words, these functions can be used
' to create or decode attachments in email. They could also be used to send any type
' of file in the form of text pasted into an email. If the recipient has the decode script
' they can just select and copy the email content, paste it into Notepad, save it as a
' TXT file, then drop it onto the script to convert that text into the original JPG, EXE, or
' any other file type.
Dim FSO, TS, sIn, sOut, Arg, IfEncode, OFil, LSize, LRet
Arg = WScript.Arguments(0)
LRet = MsgBox("Click yes to encode file or no to decode.", 36)
If LRet = 6 Then
IfEncode = True
Else
IfEncode = False
End If
Set FSO = CreateObject("Scripting.FileSystemObject")
Set OFil = FSO.GetFile(Arg)
LSize = OFil.Size
Set OFil = Nothing
Set TS = FSO.OpenTextFile(Arg)
sIn = TS.Read(LSize)
Set TS = Nothing
If IfEncode = True Then
sOut = ConvertToBase64(sIn, True)
Set TS = FSO.CreateTextFile(Arg & "-64", True)
TS.Write sOut
TS.Close
Set TS = Nothing
Else
sOut = DecodeBase64(sIn)
Set TS = FSO.CreateTextFile(Arg & "-de64", True)
TS.Write sOut
TS.Close
Set TS = Nothing
End If
Set FSO = Nothing
MsgBox "Done."
'------------------------------------------------------
Function ConvertToBase64(sBytes, AddReturns)
Dim B2(), B76(), ABytes(), ANums
Dim i1, i2, i3, LenA, NumReturns, sRet
On Error Resume Next
ANums = Array(65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47)
LenA = Len(sBytes)
'-- convert each string character to ASCII value.
ReDim ABytes(LenA - 1)
For i1 = 1 to LenA
ABytes(i1 - 1) = Asc(Mid(sBytes, i1, 1))
Next
'-- generate base 64 equivalent in array B2.
ReDim Preserve ABytes(((LenA - 1) \ 3) * 3 + 2)
ReDim Preserve B2((UBound(ABytes) \ 3) * 4 + 3)
i2 = 0
For i1 = 0 To (UBound(ABytes) - 1) Step 3
B2(i2) = ANums(ABytes(i1) \ 4)
i2 = i2 + 1
B2(i2) = ANums((ABytes(i1 + 1) \ 16) Or (ABytes(i1) And 3) * 16)
i2 = i2 + 1
B2(i2) = ANums((ABytes(i1 + 2) \ 64) Or (ABytes(i1 + 1) And 15) * 4)
i2 = i2 + 1
B2(i2) = ANums(ABytes(i1 + 2) And 63)
i2 = i2 + 1
Next
For i1 = 1 To i1 - LenA
B2(UBound(B2) - i1 + 1) = 61 ' add = signs at end if necessary.
Next
'-- Most email programs use a maximum of 76 characters per line when encoding
'-- binary files as base 64. This next function achieves that by generating another
'--- array big enough for the added vbCrLfs, then copying the base 64 array over.
If (AddReturns = True) And (LenA > 76) Then
NumReturns = ((UBound(B2) + 1) \ 76)
LenA = (UBound(B2) + (NumReturns * 2)) '--make B76 B2 plus 2 spots for each vbcrlf.
ReDim B76(LenA)
i2 = 0
i3 = 0
For i1 = 0 To UBound(B2)
B76(i2) = B2(i1)
i2 = i2 + 1
i3 = i3 + 1
If (i3 = 76) And (i2 < (LenA - 2)) Then '--extra check. make sure there are still
B76(i2) = 13 '-- 2 spots left for return if at end.
B76(i2 + 1) = 10
i2 = i2 + 2
i3 = 0
End If
Next
For i1 = 0 to UBound(B76)
B76(i1) = Chr(B76(i1))
Next
sRet = Join(B76, "")
Else
For i1 = 0 to UBound(B2)
B2(i1) = Chr(B2(i1))
Next
sRet = Join(B2, "")
End If
ConvertToBase64 = sRet
End Function
Function DecodeBase64(Str64)
Dim B1(), B2()
Dim i1, i2, i3, LLen, UNum, s2, sRet, ANums
Dim A255(255)
On Error Resume Next
ANums = Array(65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47)
For i1 = 0 To 255
A255(i1) = 64
Next
For i1 = 0 To 63
A255(ANums(i1)) = i1
Next
s2 = Replace(Str64, vbCr, "")
s2 = Replace(s2, vbLf, "")
s2 = Replace(s2, " ", "")
s2 = Trim(s2)
LLen = Len(s2)
ReDim B1(LLen - 1)
For i1 = 1 to LLen
B1(i1 - 1) = Asc(Mid(s2, i1, 1))
Next
'--B1 is now in-string as array.
ReDim B2((LLen \ 4) * 3 - 1)
i2 = 0
For i1 = 0 To UBound(B1) Step 4
B2(i2) = (A255(B1(i1)) * 4) Or (A255(B1(i1 + 1)) \ 16)
i2 = i2 + 1
B2(i2) = (A255(B1(i1 + 1)) And 15) * 16 Or (A255(B1(i1 + 2)) \ 4)
i2 = i2 + 1
B2(i2) = (A255(B1(i1 + 2)) And 3) * 64 Or A255(B1(i1 + 3))
i2 = i2 + 1
Next
If B1(LLen - 2) = 61 Then
i2 = 2
ElseIf B1(LLen - 1) = 61 Then
i2 = 1
Else
i2 = 0
End If
UNum = UBound(B2) - i2
ReDim Preserve B2(UNum)
For i1 = 0 to UBound(B2)
B2(i1) = Chr(B2(i1))
Next
DecodeBase64 = Join(B2, "")
End Function
還有一個更簡單的(和更快)方法將二進制數據轉換爲Base64在VBScript比使用中發現的代碼@Hackoo。您可以通過使用MSXML2.DOMDocument
類來利用Microsoft的Base64實現。這是一個腳本,它需要一個二進制文件c:\test.jpg
並將其轉換爲Base64。生成的Base64編碼的字符串保存在文本文件(c:\out.txt
)中。它使用ADO Stream
將文件讀入二進制數組,然後將其傳遞給使用DOMDocument
將該二進制數據轉換爲Base64編碼文本的例程。
Const BINARY_FILE = "c:\test.jpg"
Const BASE64_FILE = "c:\out.txt"
With CreateObject("Scripting.FileSystemObject").CreateTextFile(BASE64_FILE, True)
.Write BinaryFileToBase64(BINARY_FILE)
.Close
End With
Function BinaryFileToBase64(strFileName)
With CreateObject("ADODB.Stream")
.Type = 1 ' Specify binary data (adTypeBinary)
.Open
.LoadFromFile strFileName
BinaryFileToBase64 = Base64Encode(.Read) ' Read binary contents into VT_UI1 | VT_ARRAY
End With
End Function
' This function accepts binary (VT_UI1 | VT_ARRAY) data and converts it to Base64-encoded text (Unicode string).
Function Base64Encode(BinaryData) ' As String
With CreateObject("MSXML2.DOMDocument.3.0").CreateElement("Base64")
.DataType = "bin.base64" ' Set the type of data the element should store
.NodeTypedValue = BinaryData ' Write the binary data
Base64Encode = .Text ' Read it back as text
End With
End Function
因此,您可以使用此腳本將任何二進制文件轉換爲其Base64編碼的字符串表示形式。例如,這是堆棧溢出的圖標,保存爲12x15位:
Qk1SAgAAAAAAADYAAAAoAAAADAAAAA8AAAABABgAAAAAABwCAAAAAAAAAAAAAAAAAAAAAAAA
hoOChoOChoOChoOChoOChoOChoOChoOChoOChoOC3uPl3uPlhoOC3uPl3uPl3uPl3uPl3uPl
3uPl3uPl3uPlhoOC3uPl3uPlhoOC3uPlhoOChoOChoOChoOChoOChoOC3uPlhoOC3uPl3uPl
hoOC3uPl3uPl3uPl3uPl3uPl3uPl3uPl3uPlhoOC3uPl3uPlhoOC3uPlcIyocIyocIyocIyo
cIyocIyo3uPlhoOC3uPl3uPlhoOC3uPl3uPl3uPl3uPl3uPlwtTdn8DV3uPlhoOC3uPl3uPl
3uPl3uPl2uHknL/UcafJVpfCVJbCbKPI3uPl3uPl3uPl3uPl3+Tm3+TmVZfCXJvEeKvLr8na
3+Tmbq7cVKHZ3+Tm3+Tm3+Tm4eXn4eXn3ePm4eXn4eXnsM3iP5fYQZjXs8/jV6Tx097o4eXn
4ubo4ubo4ubo3uToY6jbN5PXdbLc3eToOJb0K4/0e63vdqrw4+fp4+fp4+fpQZjYRZvYwNbl
3uXpOJb0LZD00t7qMYP1lLvu5Ojq5Ojq5OjqxNjm5Ojq3+bqOpf0LpH01uHrcafwPInz5Ojq
5enr5enr5enr5enr5enrRJzzLpH01+Lr3OTrMIP1psbu5enr5+rs5+rs5+rs5+rs5+rsrs7u
3eXs5+rsZqLyQ4705+rs5+rs6Ovt6Ovt6Ovt6Ovt6Ovt6Ovt6Ovt6OvtN4f0s83v6Ovt6Ovt
要解碼的Base64編碼的字符串,我們只需要執行相反的步驟。首先,我們將文本解碼爲原始的二進制形式。然後,我們將該二進制數據寫入文件。
CONST NEW_BINARY_FILE = "c:\test2.jpg"
With CreateObject("Scripting.FileSystemObject").OpenTextFile(BASE64_FILE)
Base64ToBinaryFile .ReadAll(), NEW_BINARY_FILE
.Close
End With
Sub Base64ToBinaryFile(strBase64, strFileName)
With CreateObject("ADODB.Stream")
.Type = 1 ' adTypeBinary
.Open
.Write Base64Decode(strBase64) ' Write the byte array
.SaveToFile strFileName, 2 ' Overwrite if file exists (adSaveCreateOverWrite)
End With
End Sub
Function Base64Decode(ByVal strText) ' As ByteArray
With CreateObject("MSXML2.DOMDocument.3.0").CreateElement("Base64")
.DataType = "bin.base64"
.Text = strText
Base64Decode = .NodeTypedValue
End With
End Function
所以,回到你原來的問題,你如何嵌入您的VBScript文件二進制(ICO
)文件?您可以在某處添加Base64字符串。把它放在最後,開始,在中間的某個地方。但是當然,它需要被註釋掉,因爲它不是有效的VBScript。你可能想要添加一個開始和結束分隔符,以便知道它開始和結束的位置。例如:
' Read ourself...
With CreateObject("Scripting.FileSystemObject").OpenTextFile(WScript.ScriptFullName)
' Look for the "start"...
Do Until .AtEndOfStream
strLine = .ReadLine()
If strLine = "' ~END~" Then fRead = False
If fRead Then strBase64 = strBase64 & Mid(strLine, 3)
If strLine = "' ~START~" Then fRead = True
Loop
End With
' Re-create our bitmap!
Base64ToBinaryFile strBase64, "c:\stack_overflow.bmp"
' ~START~
' Qk1SAgAAAAAAADYAAAAoAAAADAAAAA8AAAABABgAAAAAABwCAAAAAAAAAAAAAAAAAAAAAAAA
' hoOChoOChoOChoOChoOChoOChoOChoOChoOChoOC3uPl3uPlhoOC3uPl3uPl3uPl3uPl3uPl
' 3uPl3uPl3uPlhoOC3uPl3uPlhoOC3uPlhoOChoOChoOChoOChoOChoOC3uPlhoOC3uPl3uPl
' hoOC3uPl3uPl3uPl3uPl3uPl3uPl3uPl3uPlhoOC3uPl3uPlhoOC3uPlcIyocIyocIyocIyo
' cIyocIyo3uPlhoOC3uPl3uPlhoOC3uPl3uPl3uPl3uPl3uPlwtTdn8DV3uPlhoOC3uPl3uPl
' 3uPl3uPl2uHknL/UcafJVpfCVJbCbKPI3uPl3uPl3uPl3uPl3+Tm3+TmVZfCXJvEeKvLr8na
' 3+Tmbq7cVKHZ3+Tm3+Tm3+Tm4eXn4eXn3ePm4eXn4eXnsM3iP5fYQZjXs8/jV6Tx097o4eXn
' 4ubo4ubo4ubo3uToY6jbN5PXdbLc3eToOJb0K4/0e63vdqrw4+fp4+fp4+fpQZjYRZvYwNbl
' 3uXpOJb0LZD00t7qMYP1lLvu5Ojq5Ojq5OjqxNjm5Ojq3+bqOpf0LpH01uHrcafwPInz5Ojq
' 5enr5enr5enr5enr5enrRJzzLpH01+Lr3OTrMIP1psbu5enr5+rs5+rs5+rs5+rs5+rsrs7u
' 3eXs5+rsZqLyQ4705+rs5+rs6Ovt6Ovt6Ovt6Ovt6Ovt6Ovt6Ovt6OvtN4f0s83v6Ovt6Ovt
' ~END~
有可能是使用Windows內置的CERTUTIL命令更簡單的方法:
- 編碼的圖標(或其他二進制)與CERTUTIL文件(CERTUTIL -encode icon.ico圖標.b64)
- 添加的Base64編碼腳本包括
'
前綴(REM) 使用下面的代碼刪除REM和解碼中的Base64編碼轉換成二進制:
dim fso : set fso=CreateObject("scripting.FileSystemObject") dim wsh : set wsh=CreateObject("wscript.shell") '--- Extract ICO file... iconFile=fso.GetSpecialFolder(2) & "\icon" set f=fso.OpenTextFile(WScript.ScriptFullName) s=replace(f.ReadAll,"' ","") f.close set f=fso.OpenTextFile(iconFile & ".tmp",2,TRUE) f.writeline(s) f.close wsh.run "certutil -decode " & iconFile & ".tmp" & " " & iconFile & ".ico",0,true ' --- This is the output of the CERTUTIL encode command: ' -----BEGIN CERTIFICATE----- ' AAABAAYAEBAAAAAACABoBQAAZgAAACAgAAAAAAgAqAgAAM4FAAAwMAAAAAAIAKgO ' AAB2DgAAEBAAAAAAIABoBAAAHh0AACAgAAAAACAAqBAAAIYhAAAwMAAAAAAgAKgl ' .. ' .. ' AAAAHwAA/AAAAAA/AAD+AAAAAH8AAP+AAAAA/wAA/8AAAAP/AAD/4AAAB/8AAP/4 ' AAAf/wAA//4AAH//AAD//8AD//8AAA== ' -----END CERTIFICATE-----
- 1. 是否可以將文檔字段存儲在SearchKit索引中?
- 2. 是否可以將資源存儲在內存緩存中?
- 3. 在vbscript/classicasp中添加UTF-8文件 - 是否可以完成?
- 4. 是否可以在存儲過程中調用PHP文件?
- 5. 是否可以將HHVM字節碼存儲爲文件?
- 6. 如何將多個.ico文件存儲到.DLL文件
- 7. 是否可以在NSManagedObject中存儲塊?
- 8. 是否可以通過URI引用存儲在獨立存儲中的文件?
- 9. 是否可以在iPhone本地存儲中存儲音樂文件?
- 10. 是否有可能將UIButton存儲在plist文件中?
- 11. 是否可以將Audit4j審計事件存儲到MongoDB中?
- 12. 是否可以讀取存儲在計算機上的文件?
- 13. 是否可以從git存儲中刪除單個文件?
- 14. 是否可以在列存儲中只存儲一次列值?
- 15. Three.js - 是否可以將BoxGeometries或MeshLambertMaterial存儲在數組中
- 16. 是否可以將數據存儲在solr中?
- 17. 是否可以將值存儲在另一個操作localdatasheet中?
- 18. 是否可以將整體答案存儲在數據庫中?
- 19. 是否可以將圖像存儲在Flash的SharedObject中?
- 20. 是否可以將MySQL數據庫轉儲到seed.rb文件中?
- 21. csv.DictReader是否將文件存儲在內存中?
- 22. 可以將zip文件和圖像存儲在AppFabric緩存中
- 23. 是否可以將生成配置存儲在解決方案文件以外的文件中?
- 24. 是否可以使用Azure文件存儲創建Azure CDN?
- 25. 是否可以將FLV文件嵌入到閃存中?
- 26. 是否可以將任意數據保存到C#文件中?
- 27. 是否可以將Excel文件加載到內存中?
- 28. 在VBscript中是否存在「\ n」相等?
- 29. 是否可以將圖像作爲字符串存儲在可可中?
- 30. Akka actor是否可以將其傳出郵件存儲在持久郵箱中?
我能想到一些方法。如果您使用'WSF',則可以將二進制數據作爲Base64嵌入到'CDATA'部分。或者,您可以使用常規的'VBS'文件並在腳本之後嵌入Base64作爲註釋。無論哪種情況,您只需要讀取Base64並將其轉換爲二進制文件並將其寫入「ICO」文件即可。 – Bond 2014-10-29 19:00:23
感謝您的快速響應。你知道我在哪裏可以找到你提到的第二個選項的例子嗎?我試着用谷歌搜索它,但我迄今爲止只找到方法將Base64數據轉換爲文本字符串。 – user4195477 2014-10-29 21:10:19