Rechercher dans un fichier
Bonjour à tous,
Je souhaiterais rechercher des positions dans un fichier binaire et noter le début de chaque position, puis la longueur entre 2 positions.
J'ai commencé à faire obtenir la 1ère position, la 2ème puis la longueur, mais je sèche pour continuer jusqu'au bout du fichier. En obtenant les positions et longueurs, je peux récupérer des fichiers (png, doc, ect) contenu dans le bin...
Voici mon code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
'Tableau recherché
Dim list As Byte() = New Byte() {80, 75, 3, 4, 20, 0, 0, 0, 8, 0}
Dim fic As Byte() = IO.File.ReadAllBytes("C:\20131021.bin")
Dim idx As Integer = FindSequence(fic, list, 0)
Console.WriteLine("Position de départ : " & idx)
If idx = 0 Then
idx = FindSequence(fic, list, 9)
End If
Console.WriteLine("2ème Position de départ : " & idx)
'--
Dim b() As Byte = Nothing
Using fs As New FileStream("C:\20131021.bin", FileMode.Open)
Using rdr As New BinaryReader(fs)
Dim c As String = fs.Seek(0, SeekOrigin.Begin)
b = rdr.ReadBytes(idx)
IO.File.WriteAllBytes("C:\20131021.png", b)
End Using
End Using |
et
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
Public Function FindSequence(ByVal list() As Byte, ByVal value() As Byte, ByVal startingpos As Long) As Integer
Dim startIndex As Integer = Array.IndexOf(list, value(startingpos))
Do Until startIndex = -1 OrElse list.Length - startIndex <
value.Length
Dim runLength As Integer = 0
For index As Integer = 0 To value.Length - 1
If value(index) <> list(startIndex + index) Then Exit For
runLength += 1
Next
If runLength = value.Length Then Return startIndex
startIndex = Array.IndexOf(list, value(0), startIndex +
runLength)
Loop
Return -1
End Function |
Pouvez vous m'aider ou me mettre sur la voie afin que je puisse traiter tout le fichier C:\20131021.bin, rechercher sur des bytes et continuer jusqu'au bout ?
Merci à tous :ccool:
Programmation structurée !!!
Je ne réponds pas à votre demande mais mes cheveux se sont hérissés quand j'ai vu la fonction. :oops:
Ce qui suit est plus lisible et surtout compréhensible. ;)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| Public Function FindSequence(ByVal list() As Byte, ByVal value() As Byte, ByVal startingpos As Long) As Integer
Dim runLength As Integer
Dim ValeurDeRetour As Integer
Dim startIndex As Integer = Array.IndexOf(list, value(startingpos))
Do Until startIndex = -1 OrElse list.Length - startIndex < value.Length Or runLength = value.Length
runLength = 0
While runLength <= value.Length - 1 And value(runLength) = list(startIndex + runLength)
runLength += 1
End While
startIndex = Array.IndexOf(list, value(0), startIndex + runLength)
Loop
If runLength = value.Length Then
ValeurDeRetour = startIndex
Else
ValeurDeRetour = -1
End If
Return ValeurDeRetour
End Function |