PHP => Inflate GZipped string coming from C# SharpZipLib?
-
11-12-2019 - |
Question
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!
Solution
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.