将二进制数据移动到特定值从一个文件到另一个文件 [英] Move binary data until specific values from one file to another

查看:59
本文介绍了将二进制数据移动到特定值从一个文件到另一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Visual Basic应用程序,并且停留在某一时刻:
我需要该应用读取文件,选择DDS字符串之前的所有内容,将其从文件中剪切并粘贴到新文件中.

I writing a Visual Basic app and stuck at one point:
I need that app read the file, select everything before DDS string, cut it from file and paste to new file.

然后在编辑DDS之后插入该标头.

Then after edit a DDS insert that header.

问题是,DDS之前的此标头的长度不固定:此类型的每个文件都有不同的标头.我试图弄乱 System.IO.FileStream ,但没有结果.

Problem is, this header before DDS have not fixed length: every file of this type have different header. I tried to mess with System.IO.FileStream but got no result.

这甚至有可能做到吗?

推荐答案

标题长度不是很多,简单的搜索模式可能就足够了.

The header length is not that much, a simple search pattern is probably enough.

将字节序列传递到 FindHeader 方法的File内部和File路径中.
它返回一个字节数组,其中包含找到指定序列之前收集的所有字节.

Pass the sequence of Bytes to find inside the File and the File path to the FindHeader method.
It returns a byte array containing all the bytes collected before the specified sequence is found.

这是一个简单的模式匹配,它会向前搜索,直到找到可以与指定序列匹配的第一个字节为止.
然后,它读取一个缓冲区,并将缓冲区与序列进行比较:
-如果匹配,则返回到该点为止累积的字节;
-如果不是,则从 [Sequence Length]-1 的当前位置(在当前Stream缓冲区内)回溯并继续.

This is a simple patter matching that seeks forward until it finds the first byte that can match the specified sequence.
It then reads a buffer and compares the buffer with the sequence:
- if it's a match, returns the bytes accumulated until that point;
- if it's not, it backtracks from the current position of a [Sequence Length] - 1 positions (inside the current Stream buffer) and continues.

您可以这样称呼它:

Dim closeSequence = New Byte() { &H44, &H44, &H53 }
Dim headerBytes = FindHeader([Source File 1 Path], closeSequence)

现在,我们有了第一个源文件的标题.

Now we have the Header of the first source file.

第二个源文件的数据部分为:

The data section of the second source file is then:

Dim sourceFile2DataStart = FindHeader([Source File 2 Path], closeSequence).Length + closeSequence.Length
Dim dataLength = New FileInfo([Source File 2 Path]).Length - sourceFile2DataStart

我们需要创建第三个文件,其中将包含第一个文件的标题和从第二个文件读取的数据.

We need to create a third file which will contain the Header of the fist file and the data read from the second file.

' Create a read buffer. The buffer length is less than or equal to the data length
Dim bufferLength As Integer = CInt(If(dataLength >= 1024, 1024, dataLength))
Dim buffer As Byte() = New Byte(bufferLength - 1) {}
Dim read As Integer = 0

使用两个FileStream对象,我们创建一个新的目标文件,写入第一个源文件的头文件 closeSequence ,该文件标识数据部分的开头,然后我们从第二个源文件中读取一个缓冲区,然后将该缓冲区写入目标文件:

Using two FileStream objects, we create a new Destination File, write the header of the first Source File, the closeSequence that identifies the start of the data section, then we read a buffer from the second Source File and write the buffer to the Destination File:

Dim patchworkFilePath as string = [Path of the Destination File]

Using sWriter As FileStream = New FileStream(patchworkFilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None),
    sReader As FileStream = New FileStream([Source File 2 Path], FileMode.Open, FileAccess.Read, FileShare.None)
    sReader.Seek(sourceFile2DataStart, SeekOrigin.Begin)
    sWriter.Write(header1Bytes, 0, header1Bytes.Length)
    sWriter.Write(closeSequence, 0, closeSequence.Length)

    While True
        read = sReader.Read(buffer, 0, buffer.Length)
        If read = 0 Then Exit While
        sWriter.Write(buffer, 0, read)
    End While
End Using


Header阅读器方法:


The Header reader method:

Public Function FindHeader(filePath As String, headerClosure As Byte()) As Byte()
    Dim byteToFind = headerClosure(0)
    Dim buffer = New Byte(headerClosure.Length - 1) {}
    Dim header = New List(Of Byte)(2048)
    Dim read As Integer = 0

    Using fs As FileStream = New FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None)
        While fs.Position <= (fs.Length - headerClosure.Length)
            read = fs.ReadByte()
            If read = byteToFind Then
                fs.Read(buffer, 1, buffer.Length - 1)
                buffer(0) = CByte(read)
                If buffer.SequenceEqual(headerClosure) Then Exit While
                fs.Seek(-(buffer.Length - 1), SeekOrigin.Current)
            End If
            header.Add(CByte(read))
        End While
    End Using
    Return header.ToArray()
End Function

这篇关于将二进制数据移动到特定值从一个文件到另一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆