繁体   English   中英

PHP(ZLIB)的C(ZLIB)压缩数组的未压缩返回乱码

[英]PHP (ZLIB) uncompression of a C (ZLIB) compressed array returns gibberish

我有一组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++;
    }

必须将未压缩的字符串切成与float的长度相对应的块,然后unpack()然后从二进制“ chunk”中重构float数据。 这是我可以为上述片段提供的最简单的描述。

compress2()产生zlib格式(RFC 1950)。 我将不得不猜测gzuncompress()期望使用gzip格式(RFC 1952)。 因此,如果找不到gzip标头,gzuncompress()将立即失败。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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