Home  >  Q&A  >  body text

c++ - zlib 解压gzip/deflate 报错问题?

加入http头:wstring szHeader = L"Accept-Encoding: gzip,deflate";

用网上提供的这段代码可以成功解压http gzip压缩的网页 ,比如http://www.sohu.com/可以成功解压

但是压解deflate压缩的网页不行(比如这个网站:http://122.114.115.247:8080/?...,报错Z_DATA_ERROR

代码如下:

/* HTTP gzip decompress */
/* 参数1.压缩数据 2.数据长度 3.解压数据 4.解压后长度 */
int HttpRequests::httpgzdecompress(Byte* zdata, uLong nzdata,
    Byte* data, uLong* ndata)
{
    int err = 0;
    z_stream d_stream = {
        0
    }; /* decompression stream */
    static char dummy_head[2] = {
        0x8 + 0x7 * 0x10,
        (((0x8 + 0x7 * 0x10) * 0x100 + 30) / 31 * 31) & 0xFF, 
    };
    d_stream.zalloc = (alloc_func) 0;
    d_stream.zfree = (free_func) 0;
    d_stream.opaque = (voidpf) 0;
    d_stream.next_in = zdata;
    d_stream.avail_in = 0;
    d_stream.next_out = data;
    if (inflateInit2(&d_stream, 47) != Z_OK)
        return(-1);
    while (d_stream.total_out < *ndata && d_stream.total_in < nzdata) {
        d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
        if ((err = inflate(&d_stream, Z_NO_FLUSH)) == Z_STREAM_END)
            break;
        if (err != Z_OK) {
            if (err == Z_DATA_ERROR) {
                d_stream.next_in = (Bytef *) dummy_head;
                d_stream.avail_in = sizeof(dummy_head);
                if ((err = inflate(&d_stream, Z_NO_FLUSH)) != Z_OK) {
                    return(-1);
                }
            } else
                return(-1);
        }
    }
    if (inflateEnd(&d_stream) != Z_OK)
        return(-1);
    *ndata = d_stream.total_out;
    return(0);
}

解压deflate压缩网页不行,gzip可以,

PHP中文网PHP中文网2764 days ago883

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 14:35:39

    Should you use deflateInit to decompress? How is your code inflateInit2

    reply
    0
  • Cancelreply