PHP => Inflate a GZipped string coming from C # SharpZipLib?

I have a C # application where I use SharpZipLib to deflate a very long string, and then send the data to the PHP service in the Base64 string of the deflated byte [].

For some reason, when trying to fill it on the PHP side, it returns 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!

+4
source share
1 answer

I canโ€™t say why it fails for you with GZipOutStream , although I assume that it does something else, but only simple deflate compression. I changed my code to use DeflateStream instead of System.IO.Compression , 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 it was about using compression for the Windows Phone project, I tried using DeflateStream from SharpCompress , and it works just fine, you just need to change which namespace you use, the classes are the same.

+1
source

Source: https://habr.com/ru/post/1436976/


All Articles