Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compressed Array Bytes Java from .NET Webservice

1 - From a webservice. NET 2008 (vb), I have a method that returns an array of bytes, the byte array is actually a string "Hola Mundo" ("Hello World" in English) compressed with the Class of System.IO.Compression GZipStream.

2 - The method returns the string "Hola Mundo" compressed, and this is what the webservice returns:

<base64Binary>
H4sIAAAAAAAEAO29B2AcSZYlJi9tynt/SvVK1+B0oQiAYBMk2JBAEOzBiM3mkuwdaUcjKasqgcplVmVdZhZAzO2dvPfee++999577733ujudTif33/8/XGZkAWz2zkrayZ4hgKrIHz9+fB8/Ir5dlVn6xXo5q/4f0m5DIgoAAAA=
</base64Binary>

3 - if I do a test from a windows application from Visual Basic. NET to run this method returns me this string and Unzip with another function I have, it brings me the "Hola Mundo" ....

4 - On Android (Eclipse) and I managed to make the request and bring me the previous string ... but do not know how to decompress and show me "Hola Mundo" ...

5 - I have tried several codes from the web, but none work.

anyone know anything about this? thank you very much from now.

Greetings.

like image 424
seba123neo Avatar asked Nov 29 '25 17:11

seba123neo


2 Answers

If Android supports java.util.zip.GZIPInputStream, that's what you want.

For example:

byte[] bytes = getBytesFromWebService();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
GZIPInputStream gzip = new GZIPInputStream(bais);
try {
  InputStreamReader reader = new InputStreamReader(gzip, "UTF-8");
  try {
    String firstLine = new BufferedReader(reader).readLine();
    ...
  } finally {
    reader.close();
  }
} finally {
  gzip.close();
}
like image 126
Jon Skeet Avatar answered Dec 02 '25 05:12

Jon Skeet


I can't comment on Android, but you just need to:

  • reverse the base-64
  • reverse the gzip
  • decode the string (presumably as UTF8)

In C#, this would be something like:

string base64 = "H4sIAAAAAAAEAO29B2AcSZYlJi9tynt/SvVK1+B0oQiAYBMk2JBAEOzBiM3mkuwdaUcjKasqgcplVmVdZhZAzO2dvPfee++999577733ujudTif33/8/XGZkAWz2zkrayZ4hgKrIHz9+fB8/Ir5dlVn6xXo5q/4f0m5DIgoAAAA=";
byte[] blob = Convert.FromBase64String(base64);
string orig;
using (var ms = new MemoryStream(blob))
using (var gzip = new GZipStream(ms, CompressionMode.Decompress))
using (var reader = new StreamReader(gzip))
{
    orig = reader.ReadToEnd(); // Hola Mundo
}
like image 29
Marc Gravell Avatar answered Dec 02 '25 07:12

Marc Gravell