Frage

I'm migrating a web service that was developed in VB.NET to PHP

I explain:

In VB. NET I have a method that compresses a single string with GZIP. ("Hello world!")

The method in the web service returns an array of bytes.

Then the array of bytes is received on a device with android, decompressed and converted to a string, this process works perfect.

the method in VB.NET, is this:

<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.

the tests I've done in PHP returns me the following.

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

the value returned in PHP is:

80jNyclXKM8vyklRBAA=

Why ? There is an example that returns the same ?

Thanks in advance for all.

War es hilfreich?

Lösung

You are using the wrong de-/compression algorithm. Use phps gzcompress() and gzuncompress() instead.

Andere Tipps

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. Neither gzdeflate nor gzcompress will do that. The former produces raw deflate streams, and the second zlib streams. (Don't get me started about the misleading names and the messed up PHP documentation about them.)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top