Android Question How to decompress string with B4A?

tummosoft

Member
Licensed User
I have a vb.net function for compressing string, and return the result as a string.

B4X:
Public Shared Function Zip(text As String) As String

        Dim buffer As Byte() = System.Text.Encoding.UTF8.GetBytes(text)

        Dim ms As New MemoryStream()

        Using zipStream As New System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress, True)

            zipStream.Write(buffer, 0, buffer.Length)

        End Using

        ms.Position = 0

        Dim outStream As New MemoryStream()

        Dim compressed As Byte() = New Byte(ms.Length - 1) {}

        ms.Read(compressed, 0, compressed.Length)


        Dim gzBuffer As Byte() = New Byte(compressed.Length + 3) {}

        System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length)

        System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4)

        Return Convert.ToBase64String(gzBuffer)

    End Function

And result my compressed string: ""Hello World" -> CwAAAB+LCAAAAAAABADzSM3JyVcIzy/KSQEAVrEXSgsAAAA=

But I can not decompress my string in B4A:

B4X:
Dim str1 As String = "CwAAAB+LCAAAAAAABADzSM3JyVcIzy/KSQEAVrEXSgsAAAA="
 
   Dim SU As StringUtils
   Dim data() As Byte = SU.DecodeBase64(str1)
   fulltext = BytesToString(data, 0, data.Length, "UTF8")
 

Johan Schoeman

Expert
Licensed User
Something like this - see attached project with some inline Java code. It will print the encoded and decoded results to the B4A log. It requires the JavaObject library and a B4A version that supports inline Java code. Hope it helps....;)
 

Attachments

  • b4aBase64EncodeDecodeString.zip
    7.2 KB · Views: 136
Upvote 0
Top