开发者

Compressed Array Bytes Java from .NET Webservice

开发者 https://www.devze.com 2023-01-26 12:32 出处:网络
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 Syste

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/XGZkAWz开发者_如何学C2zkrayZ4hgKrIHz9+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.


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();
}


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
}


There is no base-64 conversion available in Android out of the package. I used the one available in the Bouncy Castle Java Cryptographic Library. Bouncy Castle

Also you can find them in the ksoap2-android library.


To expand on Eli's post, ksoap2-android has byte[] org.kobjects.base64.Base64.decode(String arg0)

You can get it here: http://code.google.com/p/ksoap2-android/wiki/HowToUse?tm=2

0

精彩评论

暂无评论...
验证码 换一张
取 消