4

i have a C# application where i am using SharpZipLib to deflate a very long string and then send the data over to a PHP Service in a Base64 string of the deflated byte[].

For some reason, when trying to Inflate it on the PHP side, it is returning an error: 'gzinflate: data error'.

How to inflate a gzipped string in PHP?

Here is the C# code:

    byte[] sIn = System.Text.UTF8Encoding.UTF8.GetBytes(data);

    MemoryStream rawDataStream = new MemoryStream();
    GZipOutputStream gzipOut = new GZipOutputStream(rawDataStream);
    gzipOut.IsStreamOwner = false;

    gzipOut.Write(sIn, 0, sIn.Length);
    gzipOut.Close();

    byte[] compressed = rawDataStream.ToArray();

    // data sent to the php service
    string b64 = Convert.ToBase64String(compressed);

PHP code:

    $inflated = base64_decode($_POST['data']);

    // crash here
    $inflated = gzinflate($inflated);

Thanks in advance!

Amal Dev
  • 1,938
  • 1
  • 14
  • 26
SlashJ
  • 687
  • 1
  • 11
  • 31
  • There is a [`gzdecode()`](http://php.net/gzdecode) function in more recent PHP versions, alternatively in the PHP manual comment section, or via "upgradephp". – mario Sep 29 '12 at 20:11
  • possible duplicate of [Uncompress gzip compressed http response](http://stackoverflow.com/questions/8895852/uncompress-gzip-compressed-http-response) – mario Sep 29 '12 at 20:16
  • Hi, thanks for the replies! I forgot to mention that i am developing in WAMP, not sure if it changes anything tho. – SlashJ Sep 29 '12 at 20:20

1 Answers1

1

Can't really say why it fails for you with GZipOutStream though I'm guessing it is doing something else then just a pure deflate-compression. I changed your code to use DeflateStream from System.IO.Compression instead and then it worked like a charm.

byte[] sIn = UTF8Encoding.UTF8.GetBytes("testing some shit");

MemoryStream rawDataStream = new MemoryStream();
DeflateStream gzipOut = new DeflateStream(rawDataStream, CompressionMode.Compress);

gzipOut.Write(sIn, 0, sIn.Length);
gzipOut.Close();

byte[] compressed = rawDataStream.ToArray();

// data sent to the php service
string b64 = Convert.ToBase64String(compressed);

Edit

Since the question was about using compression for a Windows Phone project I tried using the DeflateStream from SharpCompress as well and it works just fine, you just have to change which namespace you are using, the classes are the same.

Karl-Johan Sjögren
  • 16,544
  • 7
  • 59
  • 68
  • Hi, i am developing the C# application in 'Windows Phone', and DeflateStream is not available unfortunatly. :( – SlashJ Sep 29 '12 at 20:22
  • Ok, that was some crucial information. However, I tested the same code using http://sharpcompress.codeplex.com/ and it seems to inflate just fine in my php file using that one as well. Will update my answer with it as well. – Karl-Johan Sjögren Sep 29 '12 at 20:31