|
-
Nov 14th, 2010, 03:27 PM
#1
Thread Starter
Lively Member
[RESOLVED] Delete data from end of file
Hello
I have JPG images with data stored at the end of them. My goal is to simply remove this data from the end of the file. The extra data is in plain text. The JPG images can still be viewed as normal with this data in them.
The plain text in the end of the file starts with the beginning mark of
"--------data---------", a line break, and then the data. Then there is
the end mark of the string
"-------enddata-------"
How can I remove all of this this data without destroying the file? Is there a way to do it without completely copying the file?
-
Nov 14th, 2010, 05:07 PM
#2
Re: Delete data from end of file
I think something like this should do:
vb.net Code:
Dim FileData As String
FileData = IO.File.ReadAllText("C:\myfile.jpg")
If FileData.Contains("--------data---------") Then
FileData = FileData.Substring(FileData.IndexOf("--------data---------"))
End If
IO.File.WriteAllText("C:\myfile1.jpg", FileData)
-
Nov 14th, 2010, 06:16 PM
#3
Thread Starter
Lively Member
Re: Delete data from end of file
Thanks for the reply, but it doesn't do what I need.
While your post successfully replaces the JPG with just the text, I was looking to delete the text from the JPG while preserving the JPG data in the file. My biggest issue is that the data somehow changes in the process leaving a unusable JPG file.
Any more tips would be great!
-
Nov 15th, 2010, 12:24 PM
#4
Re: Delete data from end of file
 Originally Posted by Bohlas_DuBhunkus
Thanks for the reply, but it doesn't do what I need.
While your post successfully replaces the JPG with just the text, I was looking to delete the text from the JPG while preserving the JPG data in the file. My biggest issue is that the data somehow changes in the process leaving a unusable JPG file.
Any more tips would be great!
If you remove that text using notepad (or any other text editor), does it work?
-
Nov 15th, 2010, 03:14 PM
#5
Thread Starter
Lively Member
Re: Delete data from end of file
No. Using notepad and saving somehow kills the file.
I figured out how to get what I need done. Heres the code. Thanks to the original author of the byte/string conversion functions.
code Code:
Public Sub DeleteINFO(ByVal InputtedFileName As String)
' Sepp is the ---------metadatastart---------- string (simply run through the string to byte array function
Dim Sepp As String = "45,45,45,45,45,45,45,45,45,45,109,101,116,97,100,97,116,97,115,116,97,114,116,45,45,45,45,45,45,45,45,45,45,45"
Dim EntireFileBytes() As Byte = My.Computer.FileSystem.ReadAllBytes(InputtedFileName)
Dim EntireFileText As String = ArrayToString(EntireFileBytes)
EntireFileText = Strings.Left(EntireFileText, InStr(EntireFileText, Sepp))
My.Computer.FileSystem.WriteAllBytes(InputtedFileName, StringToArray(EntireFileText), False)
End Sub
'--------------------------------------------------------------
' byte array to string, and string to byte array functions
Private Shared Function ArrayToString(ByVal bytes() As Byte, Optional ByVal format As String = Nothing) As String
If bytes.Length = 0 Then Return String.Empty
Dim sb As New System.Text.StringBuilder(bytes.Length * 4)
For Each b As Byte In bytes
sb.Append(b.ToString)
sb.Append(",")
Next
sb.Length -= 1
Return sb.ToString()
End Function
Private Shared Function StringToArray(ByVal s As String, Optional ByVal style As System.Globalization.NumberStyles = Nothing) As Byte()
If s.Length = 0 Then Return New Byte() {}
Dim values() As String = s.Split(","c)
Dim bytes(values.Length - 1) As Byte
For index As Integer = 0 To values.Length - 1
bytes(index) = Byte.Parse(values(index), style)
Next
Return bytes
End Function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|