我之前使用ADODB.Stream来读取和写入二进制文件,这是
的链接How to concatenate binary file using ADODB.stream in VBscript
它运行正常唯一的问题是在Windows 2003服务器上禁用了ADODB.stream,
还有另一种方法我可以在二进制模式下读取3个文件并将它们连接起来或者将它们存储在VBscript中的一个文件中
谢谢你 Jp答案 0 :(得分:6)
一年前我遇到过类似的问题。我们知道TextStream对象用于ANSI或Unicode文本数据,而不是二进制数据;如果流是二进制的,它们的.readAll()方法会产生损坏的输出。但有解决方法。将字符逐个读入数组可以正常工作。这应该允许您将二进制数据读入VB字符串,并将其写回磁盘。当进一步操作这样的二进制字符串时,不要忘记某些操作可能导致断字符串,因为它们仅用于文本。我总是将二进制字符串转换为整数数组,然后再使用它们。
Function readBinary(path) Dim a Dim fso Dim file Dim i Dim ts Set fso = CreateObject("Scripting.FileSystemObject") Set file = fso.getFile(path) If isNull(file) Then MsgBox("File not found: " & path) Exit Function End If Set ts = file.OpenAsTextStream() a = makeArray(file.size) i = 0 ' Do not replace the following block by readBinary = by ts.readAll(), it would result in broken output, because that method is not intended for binary data While Not ts.atEndOfStream a(i) = ts.read(1) i = i + 1 Wend ts.close readBinary = Join(a,"") End Function
Sub writeBinary(bstr, path) Dim fso Dim ts Set fso = CreateObject("Scripting.FileSystemObject") On Error Resume Next Set ts = fso.createTextFile(path) If Err.number <> 0 Then MsgBox(Err.message) Exit Sub End If On Error GoTo 0 ts.Write(bstr) ts.Close End Sub
Function makeArray(n) ' Small utility function Dim s s = Space(n) makeArray = Split(s," ") End Function
答案 1 :(得分:5)
基于Luc125和Alberto的答案,这里有2个重新设计和简化的功能:
读取功能
Function readBinary(strPath)
Dim oFSO: Set oFSO = CreateObject("Scripting.FileSystemObject")
Dim oFile: Set oFile = oFSO.GetFile(strPath)
If IsNull(oFile) Then MsgBox("File not found: " & strPath) : Exit Function
With oFile.OpenAsTextStream()
readBinary = .Read(oFile.Size)
.Close
End With
End Function
写入功能
Function writeBinary(strBinary, strPath)
Dim oFSO: Set oFSO = CreateObject("Scripting.FileSystemObject")
' below lines pupose: checks that write access is possible!
Dim oTxtStream
On Error Resume Next
Set oTxtStream = oFSO.createTextFile(strPath)
If Err.number <> 0 Then MsgBox(Err.message) : Exit Function
On Error GoTo 0
Set oTxtStream = Nothing
' end check of write access
With oFSO.createTextFile(strPath)
.Write(strBinary)
.Close
End With
End Function
答案 2 :(得分:3)
ADODB流对象是VBScript唯一读取二进制流的本机方法。如果禁用ADODB,则需要安装其他一些第三方组件才能提供相同的功能。
答案 3 :(得分:2)
可以一起读取所有字节:
Set FS = CreateObject("Scripting.FileSystemObject")
Set fil = FS.GetFile(filename)
fpga = fil.OpenAsTextStream().Read(file.Size)
答案 4 :(得分:0)
ADODB流对象是VBScript读取二进制流的唯一本机方法
Const TypeBinary = 1
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Function readBytes(file)
Dim inStream: Set inStream = WScript.CreateObject("ADODB.Stream") ' ADODB stream object used
inStream.Open ' open with no arguments makes the stream an empty container
inStream.type= TypeBinary
inStream.LoadFromFile(file)
readBytes = inStream.Read()
End Function
Sub writeBytes(file, bytes)
Dim binaryStream: Set binaryStream = CreateObject("ADODB.Stream")
binaryStream.Type = TypeBinary
binaryStream.Open 'Open the stream and write binary data
binaryStream.Write bytes
binaryStream.SaveToFile file, ForWriting 'Save binary data to disk
End Sub
答案 5 :(得分:0)
读取3个文件并加入一个文件(不包含ADODB
)
Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
If oFSO.FileExists("out.bin") Then oFSO.DeleteFile("out.bin")
Dim outFile : Set outFile = oFSO.OpenTextFile("out.bin", 8, true)
' 3 input files to concatenate
Dim oFS1 : Set oFS1 = oFSO.GetFile("in1.bin")
Dim oFS2 : Set oFS2 = oFSO.GetFile("in2.bin")
Dim oFS3 : Set oFS3 = oFSO.GetFile("in3.bin")
Dim read1 : Set read1 = oFS1.OpenAsTextStream()
Dim read2 : Set read2 = oFS2.OpenAsTextStream()
Dim read3 : Set read3 = oFS3.OpenAsTextStream()
Dim write1 : write1 = read1.Read(oFS1.Size)
read1.Close
outFile.write(write1)
Dim write2 : write2 = read2.Read(oFS2.Size)
read2.Close
outFile.write(write2)
Dim write3 : write3 = read3.Read(oFS3.Size)
read3.Close
outFile.write(write3)
outFile.Close
在Win 10上对音频,视频,图像,zip存档和pdf(二进制)进行了测试,以进行二进制文件的复制,编辑,拆分,合并,修补和(字节级别)加密,编码和压缩。
有关二进制文件修补程序,请参见example (answer) here。