简体   繁体   中英

Can PHP decompress a file compressed with the .NET GZipStream class?

I have a C# application that communicates with a PHP-based SOAP web service for updates and licensing.

I am now working on a feedback system for users to submit errors and tracelogs automatically through the software. Based on a previous question I posted, I felt that a web service would be the best way to do it (most likely to work properly with least configuration).

My current thought is to use .NET built-in gzip compression to compress the text file, convert to base64, send to the web-service, and have the PHP script convert to binary and uncompress the data.

Can PHP decompress data compressed with GZipStream, and if so, how?

I actually tried this. GZipStream doesn't work. On the other hand, compressing with DeflateStream on .NET side and decompressing with gzinflate on PHP side do work. Your mileage may vary...

If the http-level libraries implements it (Both client and server), http has support for gzip-compression , in which case there would be no reason to manually compress anything. You should check if this is already happening before you venture any further.

I was able to demo this with Gzip on C# and PHP.

Gzip Compressing in C#:

using System;
using System.IO;
using System.IO.Compression;
using System.Text;

public class Program {
    public static void Main() {
        string s = "Hi!!";

        byte[] byteArray = Encoding.UTF8.GetBytes(s);
        byte[] b2 = Compress(byteArray);

        Console.WriteLine(System.Convert.ToBase64String(b2));
    }

    public static byte[] Compress(byte[] bytes) {
        using (var memoryStream = new MemoryStream()) {
            using (var gzipStream = new GZipStream(memoryStream, CompressionLevel.Optimal)) {
                gzipStream.Write(bytes, 0, bytes.Length);
            }
            return memoryStream.ToArray();
        }
    }

    public static byte[] Decompress(byte[] bytes) {
        using (var memoryStream = new MemoryStream(bytes)) {

            using (var outputStream = new MemoryStream()) {
                using (var decompressStream = new GZipStream(memoryStream, CompressionMode.Decompress)) {
                    decompressStream.CopyTo(outputStream);
                }
                return outputStream.ToArray();
            }
        }
    }
}

the code above prints the base64 encoded compressed string which is H4sIAAAAAAAEAPPIVFQEANxaFPgEAAAA for the Hi!! input.

Here's the code to decompress in PHP:

echo gzdecode(base64_decode('H4sIAAAAAAAEAPPIVFQEANxaFPgEAAAA'));

Yes, PHP can decompress GZIP compressed strings, with or without headers.

  • gzdecode for GZIP file format (ie, compatible with gzip)
  • gzinflate for "raw" DEFLATE format
  • gzuncompress for ZLIB format (GZIP format without some header info)

I don't know for sure which one you'd want as I'm unfamiliar with .NET GZipStream. It sounds a little like gzuncompress, as the ZLIB format is kind of a "streaming" format, but try all three.

Since the server is accepting web requests you really should be checking the HTTP headers to determine if any client accepts GZIP encoding rather than just guessing and gzipping each and every time.

If the PHP client can do gzip itll set the header and your code will then react according and do the right thing. Assuming or guessing is a poor choice when the facility is provided for your code to learn the capabilities of the client.

I wrote an article I recently posted that shows how to compress/decompress in C#. I used it for almost the same scenario. I wanted to transfer log files from the client to the server and they were often quite large. However in my case my webservice was running in .NET so I could use the decompress method. But looks like PHP does support a method called gzdecode that would work.

http://coding.infoconex.com/post/2009/05/Compress-and-Decompress-using-net-framework-and-built-in-GZipStream.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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