为什么 zlib 放气初始化调用一次不起作用?

why zlib deflateInit calling once not working?

本文关键字:一次 不起作用 zlib 初始化 调用 为什么      更新时间:2023-10-16

我目前正在编写一个使用 zlib 压缩字符数组的函数。由于我想优化性能和速度,我只想调用 deflateInit(( 一次并重用 zstream 对象,因为我想避免重新分配,反复取消分配。我已经尝试了下面的方法,它只是更新 zstream 对象中的输入和输出缓冲区,但它没有从 while 循环的第二次迭代中给出正确的输出。下面是代码:

#include <stdio.h>
#include <iostream>
#include <string.h> // for strlen
#include <assert.h>
#include <chrono>
#include "zlib.h"
// adapted from: http://stackoverflow.com/questions/7540259/deflate-and-inflate-zlib-h-in-c
int main(int argc, char * argv[]) {
// original string len = 36
char a[500] = {};
for (int i = 0; i < 498; i++) {
a[i] = 'a';
}
a[499] = '';
// placeholder for the compressed (deflated) version of "a"
char b[500] = {};
// placeholder for the UNcompressed (inflated) version of "b"
char c[500] = {};
printf("Uncompressed size is: %lun", strlen(a));
printf("Uncompressed string is: %sn", a);
printf("n----------nn");
z_stream defstream;
defstream.zalloc = Z_NULL;
defstream.zfree = Z_NULL;
defstream.opaque = Z_NULL;
deflateInit( & defstream, Z_BEST_COMPRESSION);
int i = 0;
while (i < 5) {
auto t1 = std::chrono::high_resolution_clock::now();
// setup "a" as the input and "b" as the compressed output
// STEP 1.
// deflate a into b. (that is, compress a into b)
// zlib struct
int ret;
char b[500] = {};
defstream.avail_in = (uInt) strlen(a) + 1; // size of input, string + terminator
defstream.next_in = (Bytef * ) a; // input char array
defstream.avail_out = 500;
defstream.next_out = (Bytef * ) b;
ret = deflate( & defstream, Z_FINISH); /* no bad return value */
std::cout << "ret" << ret << std::endl;
std::cout << "avail_out" << defstream.avail_out << std::endl;
assert(ret != Z_STREAM_ERROR); /* state not clobbered */
// This is one way of getting the size of the output
printf("Compressed size is: %lun", strlen(b));
printf("Compressed string is: %sn", b);
std::cout << "Compressed return value is: " << ret << std::endl;
std::cout << "Avail out is: " << defstream.avail_out << std::endl;
std::cout << "Compressed length is: " << defstream.total_out << std::endl;
printf("n----------nn");
memset(b, 0, 500);

auto t2 = std::chrono::high_resolution_clock::now();
std::cout << "Timestamp: Total compression took: " <<
std::chrono::duration_cast < std::chrono::microseconds > (t2 - t1).count() <<
" microsecondsn";
i++;
}
deflateEnd( & defstream);
// inflateEnd(&infstream);
return 0;
}

输出:

----------
ret1
avail_out35
Compressed size is: 9
Compressed string is: x�KL#
0
Compressed return value is: 1
Avail out is: 35
Compressed length is: 15
----------
Timestamp: Total compression took: 103 microseconds
ret-5
avail_out50
Compressed size is: 0
Compressed string is: 
Compressed return value is: -5
Avail out is: 50
Compressed length is: 15
----------
Timestamp: Total compression took: 42 microseconds
ret-5
avail_out50
Compressed size is: 0
Compressed string is: 
Compressed return value is: -5
Avail out is: 50
Compressed length is: 15
----------
Timestamp: Total compression took: 41 microseconds
ret-5
avail_out50
Compressed size is: 0
Compressed string is: 
Compressed return value is: -5
Avail out is: 50
Compressed length is: 15
----------
Timestamp: Total compression took: 41 microseconds
ret-5
avail_out50
Compressed size is: 0
Compressed string is: 
Compressed return value is: -5
Avail out is: 50
Compressed length is: 15
----------
Timestamp: Total compression took: 42 microseconds

在这里你可以看到,我从 while 循环的第二次迭代中得到错误代码 -5 (Z_BUF_ERROR( 和空 b 数组。但是如果我在 while 循环中移动 zlib 对象声明和 deflateInit((,一切正常。有什么解释吗?

在 zlib 的文档中,他们写道:

如果参数 flush 设置为 Z_FINISH,则处理挂起的输入,刷新挂起的输出,如果

有足够的输出空间,则 Calm 返回带有 Z_STREAM_END 的压缩。如果 deflate 返回 Z_OK 或 Z_BUF_ERROR,则必须再次调用此函数,并具有 Z_FINISH 和更多的输出空间(avail_out 年更新(,但没有更多的输入数据,直到它返回并显示Z_STREAM_END或错误。放气返回Z_STREAM_END后,流上唯一可能的操作是放气重置或放气结束。

所以它需要更多的缓冲区,注意你的缓冲区是 500,但只说可用是 50,这可能是错误,否则你需要更多逻辑来添加更多缓冲区。