gpt4 book ai didi

c++ - DEFLATED 数据将始终大于未压缩的输入数据

转载 作者:行者123 更新时间:2023-11-27 23:53:20 29 4
gpt4 key购买 nike

<分区>

我现在正在尝试了解 deflate 和 inflate 是如何工作的这是一个带有 dummyFields 结构的简单程序。

// Here i give a total memory size for the output buffer used by deflate func
#define CHUNK 16384

struct dummyFields
{
long a;
char b;
long c;
float d;
float e;
float f;
float g;
float h;
char i;
unsigned int j;
};

Bytef *dataOriginal = (Bytef*)malloc( sizeof(dummyFields) );
Bytef *dataCompressed = (Bytef*)malloc( CHUNK );

z_stream s
s.zalloc = Z_NULL;
s.zfree = Z_NULL;
s.opaque = Z_NULL;
deflateInit(&s, Z_DEFAULT_COMPRESSION);

s.avail_out = CHUNK;
s.next_out = dataCompressed;

int compressSize = 0;
int decompSize = 0;
dummyFields para;

// set all values equals to 0
memset( &para, 0, sizeof(dummyFields) );
//Inserts value in struct fields
para.a = 31272;
para.b = 'z';
para.c = 66.54;
para.e = 123;
para.f = 66.54;
.
.
para.j = 123;
//copy this values in a Bytef* elements
memcpy( dataOriginal, &para, sizeof(dummyFields));

s.avail_in = sizeof(dummyFields);
s.next_in = dataOriginal;

int response = deflate(&s, Z_FINISH);

//don't get any errors here
if( res == Z_STREAM_END ){
compressSize = CHUNK - s.avail_out;
}
}
deflateEnd(&s);

//here I get 45 byte but actual struct sizeof is 40.
printf("Total bytes after compress %d\n",compressSize);

// Trying to get back my data
Bytef *decomp = (Bytef*)malloc( sizeof(Particle) );

z_stream s_inflate;
s_inflate.zalloc = Z_NULL;
s_inflate.zfree = Z_NULL;
s_inflate.opaque = Z_NULL;
inflateInit(&s_inflate);

// data i want to get at the next inflate
s_inflate.avail_in = spaceUsed;
s_inflate.next_in = dataCompressed;

s_inflate.avail_out = sizeof(dummyFields);
s_inflate.next_out = decomp;

int response = inflate( &s_inflate, Z_NO_FLUSH );

if( res == Z_STREAM_END ){
decompSize = CHUNK - s.avail_out;
}
//Here I got 40 bytes which is correct beacuse actual struct size is 40
printf("Total bytes after compress %d\n",decompSize);
inflateEnd( &s_inflate );

dummyFields data;
memset( &data, 0, sizeof(data) );
memcpy( &data, decomp, sizeof(data));

当我试图从膨胀响应中备份我的数据时,我得到了实际值(h 是正确的)。 Deflate 和 Inflate 函数工作正常。

当我尝试找到(sizeof(dummyFields)) 结构的大小时,它给了我 40 个字节

问题

  • 当我压缩数据时,struct 的实际大小是 40,它给我 45字节怎么可能?

  • 我的需求数据是 30 到 40 字节有没有其他库这会将数据压缩 10 到 20 字节(当我给 30 到 40 字节时)?

  • 有什么方法可以保证输出的压缩数据会比输入数据小?

注意

当我将结构字段的数量或数据大小从 40 字节增加到 100 字节时,压缩结果是可以的。当我将字段数或大小从 100 字节减少到 40 字节时,压缩结果不好

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