ZLIB膨胀给予“数据错误”在PHP(ZLIB inflate give 'data err

2019-10-18 05:30发布

我有一个具有zlib的文件deflate的4096个字节d块。 我能够膨胀的4096个字节与C ++,使用Minzip的至少1块inflate实现方式中,没有出现乱码文本或data error

我用下面的C ++实现膨胀的数据:

#define DEC_BUFFER_LEN 20000
int main(int argc, char* argv[]) {
    FILE *file = fopen("unpackme.3di", "rb");

    char *buffer = new char[4096];

    std::fstream outputFile;
    outputFile.open("output.txt", std::ios_base::out | std::ios_base::trunc | std::ios_base::binary);


    // Data zit nu in de buffer
    char *decbuffer = new char[DEC_BUFFER_LEN];

    mz_streamp streampie = new mz_stream();

    streampie->zalloc = Z_NULL;
    streampie->zfree = Z_NULL;
    streampie->opaque = Z_NULL;
    streampie->avail_in = Z_NULL;
    streampie->next_in = Z_NULL;

    if (inflateInit(streampie) != Z_OK)
        return -1;

    fread(buffer, 1, 4096, file);

    streampie->next_in = (Byte *)&buffer[0];
    streampie->avail_in = 4096;

    streampie->next_out = (Byte *)&decbuffer[0];
    streampie->avail_out = DEC_BUFFER_LEN;

    streampie->total_out = 0;

    int res = inflate(streampie, Z_NO_FLUSH);

    if (res != Z_OK && res != Z_STREAM_END) {
        std::cout << "Error: " << streampie->msg << std::endl;
        return;
    }
    outputFile.write(decbuffer, streampie->total_out); // Write data to file

    fclose(file);

    inflateEnd(streampie);

    outputFile.flush();
    outputFile.close();

    getchar();

    return 0;
}

和我使用下面的PHP实现:

function Unpack3DI($inputFilename) {
    $handle = fopen($inputFilename, 'rb');
    if ($handle === false) return null;

    $data = gzinflate(fread($handle, 4096));
    return $data;
}


var_dump(Unpack3DI('unpackme.3di'));

结果:

Warning: gzinflate() [function.gzinflate]: data error in /var/www/html/3di.php on line 9
bool(false)

Answer 1:

问题是,我使用了错误的功能。 我不得不使用gzuncompress而不是gzinflate 。 此外,推动整个文件中gzuncompress做的工作非常出色实际上,作为zlib的检查,如果有剩余块被压缩。

关于PHP zlib的方法的详细信息中回答了这个答案,“哪个压缩方法在PHP中使用?”



文章来源: ZLIB inflate give 'data error' in PHP
标签: php zlib inflate