Android Question Decompress base64 gziped string

tomot

New Member
Licensed User
Longtime User
Hi,
here is my problem which stopped me:
I have base64 gziped string waiting on remote server. Download is ok. I got string on device. The problem is converting it to readable text (aka json)
I made also win mobile version which already works. I used next function to decompress string:

B4X:
public static string Decompress(string s)
{
var bytes = Convert.FromBase64String(s);
using (var msi = new MemoryStream(bytes))
using (var mso = new MemoryStream())
{
using (var gs = new GZipStream(msi, CompressionMode.Decompress))
{
gs.CopyTo(mso);
}
return Encoding.Unicode.GetString(mso.ToArray(), 0, (int)mso.Length);
}
}
which returns json for parsing.
(string which I got on both device is the same (tested also with telnet so there are no differences)
Now let's go back to b4a:
I tried with two methods, both returned the same result, string with two � chars between each char. Of course json parser cannot parse that. s2 is the final string

B4X:
Dim decompressed() As Byte
Dim cs As CompressedStreams
Dim su As StringUtils
 
Dim s1 As String
Dim s2 As String
Dim b() As Byte
 
... ver 1
b = B64.DecodeStoB(s1)
decompressed = cs.DecompressBytes(b, "gzip")
s2 = BytesToString(decompressed, 0, decompressed.Length, "UTF8")
 
Log(s2)
 
... ver 2 
b = su.DecodeBase64(s1)
s2 = BytesToString(y, 0, y.Length, "UTF8")
 
Log(s2)
Correct string would be: [{"ServerName":"GAIA","Url":...
But I got in both versions: [��{��"��S��e��r��v��e��r��N��a��m��e��"��:��"��G��A��I��A��"��,��"��U��r��l��"��:...
(I tried also with different encodings but then return is in chinese chars)
Thnx, tomot
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Ok, this was challenging :)

The data is not compressed and it is encoded in UTF 16 - Little Endian:
B4X:
Dim s As String = File.ReadString(File.DirAssets, "data1.txt")
   Dim su As StringUtils
   Dim data() As Byte = su.DecodeBase64(s)
   Dim txt As String = BytesToString(data, 0, data.Length, "UTF-16LE")
   File.WriteString(File.DirApp, "1.txt", txt)
   Log(txt)
 
Upvote 0
Top