简体   繁体   English

PHP =>膨胀来自C#SharpZipLib的GZipped字符串?

[英]PHP => Inflate GZipped string coming from C# SharpZipLib?

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[]. 我有一个C#应用程序,我使用SharpZipLib来收集一个非常长的字符串,然后将数据发送到放弃字节[]的Base64字符串中的PHP服务。

For some reason, when trying to Inflate it on the PHP side, it is returning an error: 'gzinflate: data error'. 出于某种原因,当试图在PHP端对它进行膨胀时,它会返回一个错误:'gzinflate:data error'。

How to inflate a gzipped string in PHP? 如何在PHP中扩展gzip压缩字符串?

Here is the C# code: 这是C#代码:

    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: PHP代码:

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

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

Thanks in advance! 提前致谢!

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. 不能真正说明为什么GZipOutStream失败了,虽然我猜它正在做其他事情然后只是一个纯粹的deflate -compression。 I changed your code to use DeflateStream from System.IO.Compression instead and then it worked like a charm. 我将代码更改为使用System.IO.Compression DeflateStream ,然后它就像一个魅力。

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. 由于问题是关于对Windows Phone项目使用压缩,我尝试使用DeflateStreamDeflateStream ,它工作得很好,你只需要改变你正在使用的命名空间,类是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM