PHP 可以解压缩使用 .NET GZipStream 类压缩的文件吗?

发布于 2024-07-22 01:38:34 字数 282 浏览 4 评论 0原文

我有一个 C# 应用程序,它与基于 PHP 的 SOAP Web 服务进行通信以进行更新和许可。

我现在正在开发一个反馈系统,供用户通过软件自动提交错误和跟踪日志。 根据我之前发布的问题,我认为 Web 服务将是实现此目的的最佳方法(最有可能以最少的配置正常工作)。

我当前的想法是使用.NET内置的gzip压缩来压缩文本文件,转换为base64,发送到Web服务,并将PHP脚本转换为二进制并解压缩数据。

PHP 可以解压使用 GZipStream 压缩的数据吗?如果可以,如何解压?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

落在眉间の轻吻 2024-07-29 01:38:35

我实际上尝试过这个。 GZipStream 不起作用。 另一方面,在 .NET 端使用 DeflateStream 进行压缩并在 PHP 端使用 gzinflate 进行解压是可行的。 你的旅费可能会改变...

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

很酷又爱笑 2024-07-29 01:38:35

如果http级库实现了它(客户端和服务器),http 支持 gzip 压缩,在这种情况下,没有理由手动压缩任何内容。 在您进一步冒险之前,您应该检查这种情况是否已经发生。

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.

随遇而安 2024-07-29 01:38:35

是的,PHP 可以解压 GZIP 压缩字符串。 作为参考,以下是 PHP 中主要的三个 gz 解压工具之间的区别:

  • gzdecode 用于 GZIP 文件格式(带有 GZIP 页眉和页脚的 DEFLATE)
  • gzinflate 用于原始 DEFLATE 格式
  • gzuncompress 用于 ZLIB 格式(使用较小的 ZLIB 页眉和页脚 DEFLATE)

由于您想要解压缩 GZIP 格式的数据,因此您使用 gzdecode

Yes, PHP can decompress GZIP compressed strings. For reference, here are the differences between the main three gz decompression tools in PHP:

  • gzdecode for GZIP file format (DEFLATE with a GZIP header and footer)
  • gzinflate for raw DEFLATE format
  • gzuncompress for ZLIB format (DEFLATE with a smaller ZLIB header and footer)

Since you want to decompress data in the GZIP format, you use gzdecode.

自我难过 2024-07-29 01:38:35

由于服务器正在接受 Web 请求,因此您确实应该检查 HTTP 标头以确定是否有客户端接受 GZIP 编码,而不是每次都猜测和进行 gzip 压缩。

如果 PHP 客户端可以执行 gzip,它将设置标头,然后您的代码将做出相应反应并执行正确的操作。 当为您的代码提供了解客户端功能的工具时,假设或猜测是一个糟糕的选择。

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.

软甜啾 2024-07-29 01:38:35

我最近发布了一篇文章,展示了如何在 C# 中压缩/解压缩。 我将它用于几乎相同的场景。 我想将日志文件从客户端传输到服务器,它们通常很大。 然而,就我而言,我的 Web 服务在 .NET 中运行,因此我可以使用解压缩方法。 但看起来 PHP 确实支持一种名为 gzdecode 的方法,该方法可以工作。

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

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

ι不睡觉的鱼゛ 2024-07-29 01:38:35

我能够在 C# 和 PHP 上使用 Gzip 进行演示。

C# 中的 Gzip 压缩:

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();
            }
        }
    }
}

上面的代码打印 Hi!! 输入的 Base64 编码压缩字符串,即 H4sIAAAAAAAEAPPIVFQEANxaFPgEAAAA

PHP 解压代码如下:

echo gzdecode(base64_decode('H4sIAAAAAAAEAPPIVFQEANxaFPgEAAAA'));

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'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文