在 C++ 之间移动压缩字符串以及带有 zlib 的 PHP
我正在开发一个项目,其中运行 PHP 的 Windows Web 服务器通过非常慢的连接与运行用 C++ 编写的应用程序的后端 Linux 服务器进行通信。由于两台机器之间的连接非常慢,因此我想压缩它们之间移动的流量。
我已经达到了可以在 C++ 中使用 Zlib 压缩字符串、将其保存到文件、读取文件并解压缩字符串的位置,在 PHP 中也是如此。但是,如果我尝试用一种语言压缩字符串并用另一种语言解压缩它(就像现实世界中发生的那样),我会收到错误消息,指出压缩数据已损坏。我还注意到,在 C++ 中压缩的相同字符串会产生与在 PHP 中不同的文件,这使我相信 Zlib 在每种语言上使用不同的压缩算法。
我两边都使用默认设置。我用来压缩和解压缩的 C++ 是,
compress((Bytef*)compressed, (uLongf*)&compressedLength, (Bytef*)uncompressed, (uLong)uncomressedLength);
uncompress((Bytef*)uncompressed, (uLongf*)&uncomressedLength, (Bytef*)compressed, (uLong)compressedLength);
而 PHP 代码是
$compressed = gzcompress($uncompressed);
$uncompressed = gzuncompress($compressed);
为什么这些会导致不同的压缩字符串?这就是导致减压问题的原因吗?我应该做什么才能让它发挥作用?另外,我并不致力于 Zlib。 Zlib 是我最初研究发现的,但如果有更好的方法来做到这一点,我洗耳恭听。
编辑:实际上,在做了更多测试之后,C++ 似乎可以与我最初的测试用例一起工作,但并不普遍。我尝试输入“hellohellohello”,在解压缩时,它报告了 Z_DATA_ERROR 并将其解压缩为“hello”。我想这意味着我在 C++ 方面做了一些错误的事情,这可以解释为什么 PHP 不高兴解压 C++ 压缩字符串。
编辑2:我尝试了zpipe.c示例程序,它正确地解压缩了PHP压缩的字符串,并生成PHP可以解压缩的压缩字符串。显然,问题存在于我的 C++ 代码中。要么我对压缩和解压缩的使用不正确,要么我错误地读取和写入文件。压缩或解压缩程序都无法与 zpipe 正确交互。
更新:我现在已经可以使用 PHP 压缩字符串并使用 PHP 或 C++ 读取它,并且我可以使用 C++ 压缩字符串并使用 C++ 读取它,但尝试读取将其与 PHP 一起使用会导致 PHP warning: gzuncompress(): data error。可能有哪些不同之处会导致这种工作/不工作场景的组合?
I'm working on a project where a Windows web server running PHP is communicating over a very slow connection with a back end Linux server running an application written in C++. Because the connection between the two machines is so slow, I'd like to compress the traffic moving between them.
I've gotten to where I can compress a string, save it to a file, read the file, and uncompress the string in C++ using Zlib, and likewise in PHP. However, if I try to compress a string in one language and decompress it in the other (as will be happening in the real world), I get errors griping that the compressed data is corrupted. I've also noticed that the same string compressed in C++ results in a different file than in PHP, which leads me to believe that Zlib is using a different compression algorithm on each language.
I'm using default settings on both sides. The C++ I'm using to do the compression and decompression is
compress((Bytef*)compressed, (uLongf*)&compressedLength, (Bytef*)uncompressed, (uLong)uncomressedLength);
uncompress((Bytef*)uncompressed, (uLongf*)&uncomressedLength, (Bytef*)compressed, (uLong)compressedLength);
while the PHP code is
$compressed = gzcompress($uncompressed);
$uncompressed = gzuncompress($compressed);
Why are these resulting in different compressed strings? Is that what's causing the problems with decompression? What should I be doing to get this to work? Also, I'm not committed to Zlib. Zlib's what my initial research uncovered, but if there's a better way to do this, I'm all ears.
Edit: Actually, after doing a little more testing, it appears that C++ was working with my initial test case, but not universally. I tried it with the input "hellohellohello", and on decompression, it reported a Z_DATA_ERROR and decompressed it to just "hello". I guess that means I'm doing something wrong on the C++ side, which may explain why PHP is unhappy decompressing C++ compressed strings.
Edit 2: I tried out the zpipe.c sample program, and it correctly uncompresses strings compressed by PHP and produces compressed strings PHP can uncompress. Clearly, the problem(s) exist in my C++ code. Either my usage of compress and uncompress is incorrect, or I'm reading and writing the file incorrectly. Neither the compress or decompress programs interact correctly with zpipe.
Update: I've now gotten to where I can compress a string using PHP and read it with either PHP or C++, and I can compress a string with C++ and read it with C++, but attempting to read it with PHP results in PHP Warning: gzuncompress(): data error. What could be different that would cause this combination of working/not working scenarios?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Zlib 的默认压缩级别是 6 - 您可以尝试将其作为 gzcompress 上的第二个参数传递PHP。
来自 ZLIB 手册:
Zlib's default compression level is 6 - you could try passing that as the second param on gzcompress for PHP.
From the ZLIB manual: