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);
}
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.
Question
Asiri
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:
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:
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
https://www.neowin.net/forum/topic/994146-c-help-php-compatible-string-gzip/Share on other sites
2 answers to this question
Recommended Posts