gpt4 book ai didi

c++ - Qt quncompress gzip 数据

转载 作者:可可西里 更新时间:2023-11-01 14:57:23 26 4
gpt4 key购买 nike

我偶然发现了一个问题,但找不到解决方案。

所以我想做的是在 qt 中解压缩数据,使用 qUncompress(QByteArray),以 gzip 格式从 www 发送。我使用 wireshark 确定这是有效的 gzip 流,也使用 zip/rar 进行了测试,两者都可以解压缩。

到目前为止的代码是这样的:

    static const char dat[40] = {
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xaa, 0x2e, 0x2e, 0x49, 0x2c, 0x29,
0x2d, 0xb6, 0x4a, 0x4b, 0xcc, 0x29, 0x4e, 0xad, 0x05, 0x00, 0x00, 0x00, 0xff, 0xff, 0x03, 0x00,
0x2a, 0x63, 0x18, 0xc5, 0x0e, 0x00, 0x00, 0x00
};
//this data contains string: {status:false}, in gzip format
QByteArray data;
data.append( dat, sizeof(dat) );

unsigned int size = 14; //expected uncompresed size, reconstruct it BigEndianes

//prepand expected uncompressed size, last 4 byte in dat 0x0e = 14
QByteArray dataPlusSize;

dataPlusSize.append( (unsigned int)((size >> 24) & 0xFF));
dataPlusSize.append( (unsigned int)((size >> 16) & 0xFF));
dataPlusSize.append( (unsigned int)((size >> 8) & 0xFF));
dataPlusSize.append( (unsigned int)((size >> 0) & 0xFF));

QByteArray uncomp = qUncompress( dataPlusSize );
qDebug() << uncomp;

解压失败并显示:qUncompress: Z_DATA_ERROR: Input data is corrupted.

AFAIK gzip 由 10 字节的 header 、DEFLATE peyload、12 字节的尾部组成(8 字节 CRC32 + 4 字节 ISIZE - 未压缩的数据大小)。 strip 化标题和尾部应该给我留下 DEFLATE 数据流,qUncompress 产生相同的错误。

我检查了用 PHP 压缩的数据字符串,如下所示:

$stringData = gzcompress( "{status:false}", 1);

和 qUncompress 解压缩该数据。(虽然我没有看到和 gzip header ,即 ID1 = 0x1f,ID2 = 0x8b)我用调试检查了上面的代码,错误发生在:

        if (
#endif
((BITS(8) << 8) + (hold >> 8)) % 31) { //here is error, WHY? long unsigned int hold = 35615
strm->msg = (char *)"incorrect header check";
state->mode = BAD;
break;
}

inflate.c 第 610 行。

我知道 qUncompress 只是 zlib 的包装器,所以我想它应该可以毫无问题地处理 gzip。欢迎任何评论。

最好的问候

最佳答案

这是我的贡献...我开发了一个类 (QCompressor),基于 zlib 用于轻松压缩/解压缩 QByteArray使用 GZIP。

qcompressor.h:

#ifndef QCOMPRESSOR_H
#define QCOMPRESSOR_H

#include <zlib.h>
#include <QByteArray>

#define GZIP_WINDOWS_BIT 15 + 16
#define GZIP_CHUNK_SIZE 32 * 1024

class QCompressor
{
public:
static bool gzipCompress(QByteArray input, QByteArray &output, int level = -1);
static bool gzipDecompress(QByteArray input, QByteArray &output);
};

#endif // QCOMPRESSOR_H

qcompressor.cpp:

#include "qcompressor.h"

/**
* @brief Compresses the given buffer using the standard GZIP algorithm
* @param input The buffer to be compressed
* @param output The result of the compression
* @param level The compression level to be used (@c 0 = no compression, @c 9 = max, @c -1 = default)
* @return @c true if the compression was successful, @c false otherwise
*/
bool QCompressor::gzipCompress(QByteArray input, QByteArray &output, int level)
{
// Prepare output
output.clear();

// Is there something to do?
if(input.length())
{
// Declare vars
int flush = 0;

// Prepare deflater status
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;

// Initialize deflater
int ret = deflateInit2(&strm, qMax(-1, qMin(9, level)), Z_DEFLATED, GZIP_WINDOWS_BIT, 8, Z_DEFAULT_STRATEGY);

if (ret != Z_OK)
return(false);

// Prepare output
output.clear();

// Extract pointer to input data
char *input_data = input.data();
int input_data_left = input.length();

// Compress data until available
do {
// Determine current chunk size
int chunk_size = qMin(GZIP_CHUNK_SIZE, input_data_left);

// Set deflater references
strm.next_in = (unsigned char*)input_data;
strm.avail_in = chunk_size;

// Update interval variables
input_data += chunk_size;
input_data_left -= chunk_size;

// Determine if it is the last chunk
flush = (input_data_left <= 0 ? Z_FINISH : Z_NO_FLUSH);

// Deflate chunk and cumulate output
do {

// Declare vars
char out[GZIP_CHUNK_SIZE];

// Set deflater references
strm.next_out = (unsigned char*)out;
strm.avail_out = GZIP_CHUNK_SIZE;

// Try to deflate chunk
ret = deflate(&strm, flush);

// Check errors
if(ret == Z_STREAM_ERROR)
{
// Clean-up
deflateEnd(&strm);

// Return
return(false);
}

// Determine compressed size
int have = (GZIP_CHUNK_SIZE - strm.avail_out);

// Cumulate result
if(have > 0)
output.append((char*)out, have);

} while (strm.avail_out == 0);

} while (flush != Z_FINISH);

// Clean-up
(void)deflateEnd(&strm);

// Return
return(ret == Z_STREAM_END);
}
else
return(true);
}

/**
* @brief Decompresses the given buffer using the standard GZIP algorithm
* @param input The buffer to be decompressed
* @param output The result of the decompression
* @return @c true if the decompression was successfull, @c false otherwise
*/
bool QCompressor::gzipDecompress(QByteArray input, QByteArray &output)
{
// Prepare output
output.clear();

// Is there something to do?
if(input.length() > 0)
{
// Prepare inflater status
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;

// Initialize inflater
int ret = inflateInit2(&strm, GZIP_WINDOWS_BIT);

if (ret != Z_OK)
return(false);

// Extract pointer to input data
char *input_data = input.data();
int input_data_left = input.length();

// Decompress data until available
do {
// Determine current chunk size
int chunk_size = qMin(GZIP_CHUNK_SIZE, input_data_left);

// Check for termination
if(chunk_size <= 0)
break;

// Set inflater references
strm.next_in = (unsigned char*)input_data;
strm.avail_in = chunk_size;

// Update interval variables
input_data += chunk_size;
input_data_left -= chunk_size;

// Inflate chunk and cumulate output
do {

// Declare vars
char out[GZIP_CHUNK_SIZE];

// Set inflater references
strm.next_out = (unsigned char*)out;
strm.avail_out = GZIP_CHUNK_SIZE;

// Try to inflate chunk
ret = inflate(&strm, Z_NO_FLUSH);

switch (ret) {
case Z_NEED_DICT:
ret = Z_DATA_ERROR;
case Z_DATA_ERROR:
case Z_MEM_ERROR:
case Z_STREAM_ERROR:
// Clean-up
inflateEnd(&strm);

// Return
return(false);
}

// Determine decompressed size
int have = (GZIP_CHUNK_SIZE - strm.avail_out);

// Cumulate result
if(have > 0)
output.append((char*)out, have);

} while (strm.avail_out == 0);

} while (ret != Z_STREAM_END);

// Clean-up
inflateEnd(&strm);

// Return
return (ret == Z_STREAM_END);
}
else
return(true);
}

这里是我的测试程序的 main():

#include <QDebug>
#include "qcompressor.h"

int main(int argc, char *argv[])
{
Q_UNUSED(argc);
Q_UNUSED(argv);

QString initialPlainText = "This is a test program for verifying that the QCompressor class works fine!";

qDebug() << "Initial plain text is: " << initialPlainText;

QByteArray compressed;

if(QCompressor::gzipCompress(initialPlainText.toLatin1(), compressed))
{
qDebug() << "Compressed text length is:" << compressed.length();

QByteArray decompressed;

if(QCompressor::gzipDecompress(compressed, decompressed))
{
qDebug() << "Decompressed text is: " << QString::fromLatin1(decompressed);
}
else
qDebug() << "Can't decompress";
}
else
qDebug() << "Can't compress";
}

为了让它工作,您需要添加一行 LIBS += -lz 到您的 .pro 文件以针对 zlib 进行链接。

关于c++ - Qt quncompress gzip 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2690328/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com