我目前正在编写一个使用zlib压缩char数组的函数。因为我想优化性能和速度,所以我只想调用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] = '\0';
// 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: %lu\n", strlen(a));
printf("Uncompressed string is: %s\n", a);
printf("\n----------\n\n");
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: %lu\n", strlen(b));
printf("Compressed string is: %s\n", 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----------\n\n");
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() <<
" microseconds\n";
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,则处理未决的输入,刷新未决的输出,如果有足够的输出空间,则使用Z_STREAM_END返回deflate。如果deflate用Z_OK或Z_BUF_ERROR返回,则必须使用Z_FINISH和更多的输出空间(更新的avail_out)再次调用此函数,但不能再输入任何数据,直到它以Z_STREAM_END或错误返回。 deflate返回Z_STREAM_END之后,流上唯一可能的操作是deflateReset或deflateEnd。
所以它需要更多的缓冲区,注意您有500的缓冲区,但只说有效是50,这可能是错误的,否则您需要更多的逻辑来添加更多的缓冲区。