在VBscript中读写二进制文件

发布于 2024-11-08 14:56:51 字数 347 浏览 0 评论 0原文

我之前使用 ADODB.Stream 来读取和写入二进制文件,这里是

如何在 VBscript 中使用 ADODB.stream 连接二进制文件

它工作正常,唯一的问题是 ADODB.stream 在 Windows 2003 服务器上被禁用,

是否有另一种方法可以读取 3 个二进制文件模式并将它们连接起来或将它们存储在 VBscript 中的一个文件中,

谢谢 日本人

I used earlier ADODB.Stream to read and to write binary file here is the link for that

How to concatenate binary file using ADODB.stream in VBscript

it works fine the only problem is ADODB.stream is disabled on windows 2003 server,

Is there another way i can read 3 files in binary mode and concatenate them or store them in one file in VBscript

thank you
Jp

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

狼亦尘 2024-11-15 14:56:51

一年前我也遇到过类似的问题。我们知道 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

子 writeBinary(bstr, 路径) 暗淡的fso 暗淡 设置 fso = CreateObject("Scripting.FileSystemObject") 出错时继续下一步 设置 ts = fso.createTextFile(路径) 如果 Err.number <> 0 然后 MsgBox(错误消息) 退出子程序 结束如果 出错时转到 0 ts.Write(bstr) ts.关闭 End Sub

Function makeArray(n) ' 小实用函数 暗淡 s = 空间(n) makeArray = 分割(s," ") 结束功能

I had a similar problem a year ago. We know that the TextStream objects are intended for ANSI or Unicode text data, not binary data; their .readAll() method produces a corrupted output if the stream is binary. But there is workaround. Reading the characters one by one into an array works fine. This should allow you to read binary data into VB strings, and write it back to disk. When further manipulating such binary strings do not forget that certain operations may result into broken strings because they are intended for text only. I for one always convert binary strings into integer arrays before working with them.

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

随梦而飞# 2024-11-15 14:56:51

基于 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

Based on Luc125 and Alberto answers here are the 2 reworked and simplified functions:

The Read function

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

The Write 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
执笔绘流年 2024-11-15 14:56:51

ADODB 流对象是 VBScript 读取二进制流的唯一本机方法。如果禁用 ADODB,您将需要安装一些其他第三方组件才能提供相同的功能。

The ADODB stream object is VBScript's only native method of reading binary streams. If ADODB is disabled, you will need to install some other third-party component to provide the same functionality.

韶华倾负 2024-11-15 14:56:51

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

ADODB stream object is VBScript's only native method of reading binary streams

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
帅冕 2024-11-15 14:56:51

可以一起读取所有字节:

Set FS = CreateObject("Scripting.FileSystemObject")
Set fil = FS.GetFile(filename)
fpga = fil.OpenAsTextStream().Read(file.Size)

It is possible to read all bytes together:

Set FS = CreateObject("Scripting.FileSystemObject")
Set fil = FS.GetFile(filename)
fpga = fil.OpenAsTextStream().Read(file.Size)
苦妄 2024-11-15 14:56:51

读取 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

在音频、视频、图像、zip 存档和文件上进行测试Win 10 上的 pdf(二进制文件),用于二进制文件复制、编辑、分割、加入、修补和(字节级)加密、编码&压缩。

请参阅示例(答案)此处用于二进制文件修补。

Read 3 files & join to one file (without 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

Tested on audio, video, image, zip archives & pdf (binaries) on Win 10 for binary file copy, edit, split, join, patching & (byte level) encryption, encoding & compression.

See example (answer) here for binary file patching.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文