简体   繁体   English

与VB.NET类似的PHP GZIP压缩

[英]Equivalent PHP GZIP Compression like in VB.NET

I'm migrating a web service that was developed in VB.NET to PHP 我正在将用VB.NET开发的Web服务迁移到PHP

I explain: 我解释:

In VB. 在VB中。 NET I have a method that compresses a single string with GZIP . NET我有一个用GZIP 压缩单个字符串的方法。 (" Hello world! ") (“ Hello world! ”)

The method in the web service returns an array of bytes . Web服务中的方法返回一个字节数组

Then the array of bytes is received on a device with android, decompressed and converted to a string, this process works perfect. 然后在具有android的设备上接收字节数组,解压缩并转换为字符串,此过程完美无缺。

the method in VB.NET, is this: VB.NET中的方法是这样的:

<WebMethod(Description:="GZIP Test")> _
Public Function GZIP() As Byte()
    Dim vTest As String = "Hello world!"

    Dim vBuffer1() As Byte = StrToByteArray(vTest)
    Dim vBuffer2() As Byte = Compress(vBuffer1)

    Return vBuffer2
End Function

Private Function StrToByteArray(ByVal str As String) As Byte()
    Dim encoding As New System.Text.UTF8Encoding()
    Return encoding.GetBytes(str)
End Function

Private Function Compress(ByVal Bits() As Byte) As Byte()
    On Error Resume Next
    Using ms As New MemoryStream(), zipMem As New GZipStream(ms, CompressionMode.Compress, True)
        zipMem.Write(Bits, 0, Bits.Length)
        zipMem.Close()

        Return ms.ToArray
    End Using
End Function

this method returns me the following value: 此方法返回以下值:

<base64Binary>H4sIAAAAAAAEAO29B2AcSZYlJi9tynt/SvVK1+B0oQiAYBMk2JBAEOzBiM3mkuwdaUcjKasqgcplVmVdZhZAzO2dvPfee++999577733ujudTif33/8/XGZkAWz2zkrayZ4hgKrIHz9+fB8/Ir6dl2WVXlV1Oftd/x+VGYUbDAAAAA==</base64Binary>

I want PHP return me the SAME VALUE. 我希望PHP返回给我相同的价值。

the tests I've done in PHP returns me the following. 我在PHP中完成的测试返回以下内容。

function GZIP() {
      ob_start ( 'ob_gzhandler' );
      return base64_encode(gzdeflate('Hello world!', 9));
}

the value returned in PHP is: PHP中返回的值是:

80jNyclXKM8vyklRBAA=

Why ? 为什么? There is an example that returns the same ? 有一个例子返回相同的?

Thanks in advance for all. 提前感谢所有人。

You are using the wrong de-/compression algorithm. 您正在使用错误的解压缩算法。 Use phps gzcompress () and gzuncompress () instead. 请改用phps gzcompress ()和gzuncompress ()。

First off, you can't require the exact same result. 首先,您不能要求完全相同的结果。 All you can require of a lossless compressor is that it reproduce exactly the same input when decompressed. 所有你需要的无损压缩器都是它在解压缩时重现完全相同的输入。

Second, you want to use gzencode to produce gzip streams. 其次,你想使用gzencode生成gzip流。 Neither gzdeflate nor gzcompress will do that. gzdeflategzcompress都不gzdeflate The former produces raw deflate streams, and the second zlib streams. 前者产生原始的deflate流,第二个zlib流。 (Don't get me started about the misleading names and the messed up PHP documentation about them .) (不要让我开始讲述误导性的名称以及关于它们的混乱的PHP文档 。)

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

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