PHP (ZLIB) 解压 C (ZLIB) 压缩数组会返回乱码

发布于 2025-01-03 15:24:39 字数 1896 浏览 1 评论 0原文

我有一组存储在数据库中的 ZLIB 压缩/base64 编码字符串(在 C 程序中完成)。我编写了一个小型 PHP 页面,应该检索这些值并绘制它们(字符串最初是浮点数列表)。

压缩/编码的 C 程序块:(

error=compress2(comp_buffer, &comp_length,(const Bytef*)data.mz ,(uLongf)length,Z_DEFAULT_COMPRESSION); /* compression */
if (error != Z_OK) {fprintf(stderr,"zlib error..exiting"); exit(EXIT_FAILURE);}
mz_binary=g_base64_encode (comp_buffer,comp_length); /* encoding */

示例)原始输入格式:

292.1149    8379.5928
366.1519  101313.3906
367.3778   20361.8105
369.1290   17033.3223
375.4355    1159.1841
467.3191    8445.3926

每一列都被压缩/编码为单个字符串。为了重建原始数据,我使用以下代码:

//$row[4] is retrieved from the DB and contains the compressed/encoded string
$mz = base64_decode($row[4]);
$unc_mz = gzuncompress($mz);
echo $unc_mz;

但这给了我以下输出:

f6jEÍ„]EšiSE@IEfŽ

任何人都可以给我一个关于我可能缺少什么的提示/提示吗?

------ 添加信息 -----

我觉得问题来自于当前 php 将 $unc_mz 视为单个字符串的事实,而实际上我必须重新构造一个包含 X 行的数组(此输出来自 9 行文件)但是...不知道如何完成该作业。

执行此操作的 C 程序大致如下:

 uncompress( pUncompr , &uncomprLen , (const Bytef*)pDecoded , decodedSize );
 pToBeCorrected = (char *)pUncompr;
 for (n = 0; n < (2 * peaksCount); n++) {
    pPeaks[n] = (RAMPREAL) ((float *) pToBeCorrected)[n];
 } 

其中peaksCount 是输入文件中“行”的数量。


编辑(15-2-2012):我的代码的问题是我没有重建数组,固定代码如下(如果有人需要类似的代码片段,可能会很方便):

  while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    $m< = base64_decode($row[4]);
    $mz_int = gzuncompress($int);
    $max = strlen($unc_mz);
    $counter = 0;
    for ($i = 0; $i < $max; $i = $i + 4) {
      $temp= substr($unc_mz,$i,4);
      $temp = unpack("f",$temp);
      $mz_array[$counter] = $temp[1];
      $counter++;
    }

未压缩的字符串必须被切成与浮点长度相对应的块,然后 unpack() 从二进制“块”重建浮点数据。这是我可以对上面的代码片段给出的最简单的描述。

I have a set of ZLIB compressed / base64 encoded strings (done in a C program) that are stored in a database. I have written a small PHP page that should retrieve these values and plot them (the string originally was a list of floats).

Chunk of C program that compresses/encodes:

error=compress2(comp_buffer, &comp_length,(const Bytef*)data.mz ,(uLongf)length,Z_DEFAULT_COMPRESSION); /* compression */
if (error != Z_OK) {fprintf(stderr,"zlib error..exiting"); exit(EXIT_FAILURE);}
mz_binary=g_base64_encode (comp_buffer,comp_length); /* encoding */

(Example) of original input format:

292.1149    8379.5928
366.1519  101313.3906
367.3778   20361.8105
369.1290   17033.3223
375.4355    1159.1841
467.3191    8445.3926

Each column was compressed/encoded as a single string. To reconstruct the original data i am using the following code:

//$row[4] is retrieved from the DB and contains the compressed/encoded string
$mz = base64_decode($row[4]);
$unc_mz = gzuncompress($mz);
echo $unc_mz;

Yet this gives me the following output:

f6jEÍ„]EšiSE@IEfŽ

Could anyone give me a tip/hint about what I might be missing?

------ Added Information -----

I feel that the problem comes from the fact that currently php views $unc_mz as a single string while in reality i would have to re-construct an array containing X lines (this output was from a 9 line file) but... no idea how to do that assignment.

The C program that did that went roughly like this:

 uncompress( pUncompr , &uncomprLen , (const Bytef*)pDecoded , decodedSize );
 pToBeCorrected = (char *)pUncompr;
 for (n = 0; n < (2 * peaksCount); n++) {
    pPeaks[n] = (RAMPREAL) ((float *) pToBeCorrected)[n];
 } 

where peaksCount would be the amount of 'lines' in the input file.


EDIT (15-2-2012): The problem with my code was that I was not reconstructing the array, the fixed code is as follows (might be handy if someone needs a similar snippet):

  while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    $m< = base64_decode($row[4]);
    $mz_int = gzuncompress($int);
    $max = strlen($unc_mz);
    $counter = 0;
    for ($i = 0; $i < $max; $i = $i + 4) {
      $temp= substr($unc_mz,$i,4);
      $temp = unpack("f",$temp);
      $mz_array[$counter] = $temp[1];
      $counter++;
    }

The uncompressed string has to be chopped into chunks corresponding to the length of a float, unpack() then reconstructs the float data from teh binary 'chunk'. That's the simplest description that I can give for the above snippet.

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

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

发布评论

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

评论(1

千仐 2025-01-10 15:24:39

compress2() 生成 zlib 格式 (RFC 1950)。我不得不猜测名为 gzuncompress() 的东西需要 gzip 格式(RFC 1952)。因此,如果找不到 gzip 标头,gzuncompress() 将立即失败。

您需要在 zlib 中使用 deflateInit2() 来请求 deflate() 生成 gzip 格式的输出,或者在 PHP 中查找或提供需要 zlib 格式的不同函数。

compress2() produces the zlib format (RFC 1950). I would have to guess that something called gzuncompress() is expecting the gzip format (RFC 1952). So gzuncompress() would immediately fail upon not finding a gzip header.

You would need to use deflateInit2() in zlib to request that deflate() produce gzip-formatted output, or find or provide a different function in PHP that expects the zlib format.

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