• 0

[C# Help] PHP Compatible String GZip


Question

Firstly I'm not a real application developer, I primarily work on the web-side of things and most of the issues I'm dealing with are things I've never looked at before so I'm at a loss as to a solution for my current problem.

But I'm currently working on a small desktop application that periodically uploads a string to a website every 20-30 seconds, to reduce the time it takes and to reduce any latency issues I'm looking to gzip the string before sending it to a php script. I've gotten a few examples of the compression function on the C# side of things and plenty examples of gzip (de)compression in PHP, but after hours of looking I haven't found a way to get it to work with PHPs gzinflate(), gzdecode() or gzuncompress(). I was wondering if anyone has managed to get this type of marriage to work before?

The current c# compression function I'm using:

        public static string Compress(string text)
        {

            byte[] buffer = Encoding.UTF8.GetBytes(text);
            MemoryStream ms = new MemoryStream();
            using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
            {
                zip.Write(buffer, 0, buffer.Length);
            }

            ms.Position = 0;
            MemoryStream outStream = new MemoryStream();

            byte[] compressed = new byte[ms.Length];
            ms.Read(compressed, 0, compressed.Length);

            byte[] gzBuffer = new byte[compressed.Length + 4];
            System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
            System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);

            return Convert.ToBase64String(gzBuffer);
        }

Example output from the function that is sent to the PHP script via post: http://www.recount.me/gzip.txt

Which I am then trying to use any of the following functions to decode:

	echo gzinflate(base64_decode($str));
	echo '<br /><br />';
	echo gzdecode(base64_decode($str));
	echo '<br /><br />';
	echo gzuncompress(base64_decode($str));

After looking around I think the issue lies with the fact that many of the PHP functions are designed with files and because I'm using a string it has no header/footer data that it expects to see, but having never really worked with c# or gzip I have no idea how to fix the problem and any advice/help would be very much appreciated.

Thanks for reading.

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

Well, that block of C# code adds a 4-byte length to the beginning of the gzip-encoded data, which by definition includes a header and a CRC/length trailer. So of course the PHP functions won't recognize it. Cut out all of that BlockCopy crap.

Not tested, but maybe something like this:

public static string Compress(string text)
{
    byte[] buffer = Encoding.UTF8.GetBytes(text);
    string output = String.Empty;

    using (MemoryStream ms = new MemoryStream())
    {
        using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true)
        {
            zip.Write(buffer, 0, buffer.Length);
        }

        output = Convert.ToBase64String(ms.ToArray());
    }

    return output;
}

Link to comment
Share on other sites

  • 0

Thank you so much for the quick reply and even more so for a working solution...I can't believe it was that simple in the end!

I ripped out the useless parts like you suggested rather than using your code and it work perfectly. So the final solution I'm using is:

        public static string Compress(string text)
        {

            byte[] buffer = Encoding.UTF8.GetBytes(text);

            var memoryStream = new MemoryStream();
            using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
            {
                gZipStream.Write(buffer, 0, buffer.Length);
            }

            memoryStream.Position = 0;

            var compressedData = new byte[memoryStream.Length];
            memoryStream.Read(compressedData, 0, compressedData.Length);

            return Convert.ToBase64String(compressedData);
        }

And for the php side:

<?php

	$str = trim($_REQUEST['data']);
	echo gzdecode(base64_decode($str));

?>

And it's taking 70KB strings down to 10KB without any loss of data which is exactly what I was looking for.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.