Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27208

VS 2008 Convert

$
0
0
Hi all , I've been facing an annoying issue using compression and convertion. There is my full code :

Quote:

Public Class Form1
Dim str As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str1 As String = Compress(Convert.ToBase64String(IO.File.ReadAllBytes(My.Computer.FileSystem.SpecialDirectories.Deskt op & "\Junk.exe")))
Dim str2 As String = Compress(Convert.ToBase64String(IO.File.ReadAllBytes(My.Computer.FileSystem.SpecialDirectories.Deskt op & "\1.exe")))
str = str1 & "语" & str2
End Sub

Public Function Compress(ByVal data As String)

Dim Result As String = String.Empty

Try

'Convert the uncompressed string into a byte array
Dim UnZippedData As Byte() = System.Text.Encoding.UTF8.GetBytes(data)

'Compress the byte array
Dim MS As New System.IO.MemoryStream()
Dim GZip As New System.IO.Compression.GZipStream(MS, System.IO.Compression.CompressionMode.Compress)
GZip.Write(UnZippedData, 0, UnZippedData.Length)
'Don't FLUSH here - it possibly leads to data loss!
GZip.Close()

Dim ZippedData As Byte()

'Encrypt the compressed byte array, if required

ZippedData = MS.ToArray()


'Convert the compressed byte array back to a string
Result = System.Convert.ToBase64String(ZippedData)

MS.Close()
GZip.Dispose()
MS.Dispose()

Catch ex As Exception
'Keep quiet - in case of an exception an empty string is returned

End Try
Return Result
End Function
Public Function Decompress(ByVal data As String)


Dim Result As String = String.Empty

Try
Dim ZippedData As Byte()

'Convert the compressed string into a byte array and decrypt the array if required

ZippedData = System.Convert.FromBase64String(data)


'Decompress the byte array
Dim objMemStream As New IO.MemoryStream(ZippedData)
Dim objGZipStream As New System.IO.Compression.GZipStream(objMemStream, System.IO.Compression.CompressionMode.Decompress)
Dim sizeBytes(3) As Byte

objMemStream.Position = objMemStream.Length - 5
objMemStream.Read(sizeBytes, 0, 4)

Dim iOutputSize As Integer = BitConverter.ToInt32(sizeBytes, 0)

objMemStream.Position = 0

Dim UnZippedData(iOutputSize - 1) As Byte

objGZipStream.Read(UnZippedData, 0, iOutputSize)

objGZipStream.Dispose()
objMemStream.Dispose()

'Convert the decompressed byte array back to a string
Result = System.Text.Encoding.UTF8.GetString(UnZippedData)

Catch ex As Exception

End Try
Return Result
End Function

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
Dim tmp() As String = str.Split("语")

Dim str1 As String = Decompress(tmp(0))

IO.File.WriteAllBytes(My.Computer.FileSystem.SpecialDirectories.Desktop & "\tx.exe", Convert.FromBase64String(str1))

Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class

It throw : error non base64 char.

Tho , when I copy the final output text to a richtextbox and save it , it work fine. I tried to add dynamically a richtextbox to store data here and if failed. Same for string builder. Any clue ?

ps : also changed the utf8 param to default. (didn't worked.):mad:

Viewing all articles
Browse latest Browse all 27208

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>