adodb.stream vbs 截取二进制文件

adodb.stream vbs 截取二进制文件,第1张

Option Explicit

Dim blnNum, objFso, objFile, strFile, lngPosition, lngStartTime

Set objFso = CreateObject("Scripting.FileSystemObject")

With WScript

strFile = objFso.GetFile(.ScriptFullName).ParentFolder &"\FileFound.txt" '源文件

lngPosition = 134 '需要截取的数据的位置

If .Arguments.Count >1 Then

strFile = .Arguments(0)

lngPosition = .Arguments(1)

End If

If RM(strFile, "^[C-Z]:\\.+\.[a-z]+$", 0, 1) = False Then

.Echo "第一个参数必须为带完整路径的文件"

Set objFso = Nothing

.Quit

End If

If objFso.FileExists(strFile) = False Then

.Echo "文件不存在..."

Set objFso = Nothing

.Quit

End If

If IsNumeric(lngPosition) = False Then

.Echo "第二个参数必须为数字"

Set objFso = Nothing

.Quit

End If

End With

lngStartTime = Timer

Dim strDstFl '输出的目标文件

strDstFl = objFso.GetFile(strFile).ParentFolder &"\" _

&objFso.GetBaseName(strFile) &"_New." _

&objFso.GetExtensionName(strFile)

Dim objADO, strRead, arrRead, i

Set objADO = CreateObject("ADODB.Stream")

With objADO

.Type = 1

.Open

End With

Dim lngSize

With CreateObject("ADODB.Stream")

.Open

.Type = 1

.LoadFromFile strFile

.Position = lngPosition

lngSize = .Size

ReDim arrRead(lngSize - lngPosition - 1)

For i = lngPosition To lngSize - 1

arrRead(i - lngPosition) = AscB(.Read(1))

Next

.Close

End With

Dim arrChrW

ReDim arrChrW((lngSize - lngPosition) \ 2)

For i = 0 To (lngSize - lngPosition - 2) Step 2

arrChrW(i \ 2) = ChrW(arrRead(i + 1) * 256 + arrRead(i))

Next

If i = UBound(arrRead) Then

arrChrW(i \ 2) = ChrW(arrRead(i))

End If

arrChrW = Join(arrChrW, "")

With CreateObject("ADODB.Stream")

.Type = 2

.Open

.WriteText arrChrW

.Position = 2

.Copyto objADO

.Close

End With

With objADO

.SaveToFile strDstFl, 2

.Close

End With

Set objADO = Nothing

Set objFso = Nothing

CreateObject("Wscript.Shell").Popup "耗时:【" &_

Round(Timer - lngStartTime, 4) &"】秒", 3, "Done...", 0

Function RM(strVar, strPtrn, blnGlb, blnIgnCase)

RM = False

Dim objReg

Set objReg = New RegExp

With objReg

.Pattern = strPtrn

.Global = blnGlb

.IgnoreCase = blnIgnCase

RM = .Test(strVar)

End With

Set objReg = Nothing

End Function

可以通过对二进制文件 *** 作(内容搜索、数据截取)后,进行修改,或者清空删除内容。

private void button2_Click(object sender, EventArgs e)

{

var list = new List<Frame>()

byte[] buf

byte[] bytStart = {0x70, 0x6D, 0x6A, 0x73, 0x7A, 0x6A, 0x66, 0x72, 0x6D}

byte[] bytEnd = { 0x70, 0x66, 0x72, 0x61, 0x6D, 0x65, 0x65, 0x6E, 0x64 }

using (var fs = new FileStream(@"D:\avi\6.lxf",FileMode.Open))

{

buf = new byte[fs.Length]

var len = (int)fs.Length

fs.Read(buf, 0, len)

}

var fStart = 0

var fEnd = 0

var offset = 0

do

{

fStart = IndexOf(buf, bytStart, offset)

fEnd = IndexOf(buf, bytEnd, fStart)

Debug.Print("{0} ~ {1}",fStart,fEnd)

if (fStart == -1||fEnd==-1)

{

offset = buf.Length

}

else

{

var frame = new Frame

{

Start = fStart + bytStart.Length,

End = fEnd,

}

frame.Length = frame.End - frame.Start

frame.Data = new byte[frame.Length]

<br> //内容截取<br> Array.Copy(buf, frame.Start, frame.Data, 0, frame.Length)

list.Add(frame)

offset = fEnd

offset += bytEnd.Length

}

}

while (offset <buf.Length)

}

查找字符串的方法

/// <summary>

/// 报告指定的 System.Byte[] 在此实例中的第一个匹配项的索引。

/// </summary>

/// <param name="srcBytes">被执行查找的 System.Byte[]。</param>

/// <param name="searchBytes">要查找的 System.Byte[]。</param>

/// <returns>如果找到该字节数组,则为 searchBytes 的索引位置;如果未找到该字节数组,则为 -1。如果 searchBytes 为 null 或者长度为0,则返回值为 -1。</returns>

internal int IndexOf(byte[] srcBytes, byte[] searchBytes,int offset = 0)

{

if (offset == -1) { return -1}

if (srcBytes == null) { return -1}

if (searchBytes == null) { return -1}

if (srcBytes.Length == 0) { return -1}

if (searchBytes.Length == 0) { return -1}

if (srcBytes.Length <searchBytes.Length) { return -1}

for (var i = offseti <srcBytes.Length - searchBytes.Lengthi++)

{

if (srcBytes[i] != searchBytes[0]) continue

if (searchBytes.Length == 1) { return i}

var flag = true

for (var j = 1j <searchBytes.Lengthj++)

{

if (srcBytes[i + j] != searchBytes[j])

{

flag = false

break

}

}

if (flag) { return i}

}

return -1

}

实本类

public class Frame

{

public int Start { getset}

public int End { getset}

public int Length { getset}

public byte[] Data { getset}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11791091.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存