在 Qt 中解压缩 Gzip Web 流

发布于 2024-11-28 19:35:45 字数 2002 浏览 0 评论 0原文

我发现这个类可以实现标题所述的功能。 但它总是抱怨段错误。 有人可以帮我调试它吗? 问候

   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;
        }

I found this class to make what the title states.
But it always complain a segfault.
Can somebody help me to debug it?
Regards

   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;
        }

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文