在Qt中解压缩Gzip Web流

时间:2011-08-11 10:09:08

标签: qt stream gzip

我发现这个课程要做出标题所说的内容。 但它总是抱怨一个段错误。 有人可以帮我调试吗? 此致

   gzipDecompress(QByteArray &compressData)
        {

            //decompress GZIP data
            //strip header and trailer
              compressData.remove(0, 10);
              compressData.chop(12);

              const int buffersize = 16384;
              quint8 buffer[buffersize];

              z_stream cmpr_stream;
              cmpr_stream.next_in = (unsigned char *)compressData.data();
              cmpr_stream.avail_in = compressData.size();
              cmpr_stream.total_in = 0;

              cmpr_stream.next_out = buffer;
              cmpr_stream.avail_out = buffersize;
              cmpr_stream.total_out = 0;

              cmpr_stream.zalloc = Z_NULL;
              cmpr_stream.zalloc = Z_NULL;

              if( inflateInit2(&cmpr_stream, -8 ) != Z_OK) {
                      qDebug() << "cmpr_stream error!";
              }

                QByteArray uncompressed;

                do {
                    qDebug()<<"decompress................"<<uncompressed.size();
                        int status = inflate( &cmpr_stream, Z_SYNC_FLUSH );

                        if(status == Z_OK || status == Z_STREAM_END) {
                                uncompressed.append(QByteArray::fromRawData(
                                     (char *)buffer,
                                     buffersize - cmpr_stream.avail_out));
                                cmpr_stream.next_out = buffer;

                                cmpr_stream.avail_out = buffersize;
                        } else {
                                 inflateEnd(&cmpr_stream);
                                }

                        if(status == Z_STREAM_END) {
                            inflateEnd(&cmpr_stream);
                            break;
                        }

                }while(cmpr_stream.avail_out == 0);

                return uncompressed;
        }

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用qUncompress