1 #include <cstring>
2 #include <cstdlib>
3 #include <iostream>
4 #include <zlib.h>
5
6 using namespace std;
7
8 int main()
9 {
10 int err;
11 Byte compr[200], uncompr[200]; // big enough
12 uLong comprLen, uncomprLen;
13 const char* hello = "12345678901234567890123456789012345678901234567890";
14
15 uLong len = strlen(hello) + 1;
16 comprLen = sizeof(compr) / sizeof(compr[0]);
17
18 err = compress(compr, &comprLen, (const Bytef*)hello, len);
19
20 if (err != Z_OK) {
21 cerr << "compess error: " << err << '/n';
22 exit(1);
23 }
24 cout << "orignal size: " << len
25 << " , compressed size : " << comprLen << '/n';
26
27 strcpy((char*)uncompr, "garbage");
28
29 err = uncompress(uncompr, &uncomprLen, compr, comprLen);
30
31 if (err != Z_OK) {
32 cerr << "uncompess error: " << err << '/n';
33 exit(1);
34 }
35 cout << "orignal size: " << len
36 << " , uncompressed size : " << uncomprLen << '/n';
37
38 if (strcmp((char*)uncompr, hello)) {
39 cerr << "BAD uncompress!!!/n";
40 exit(1);
41 } else {
42 cout << "uncompress() succeed: /n" << (char *)uncompr;
43 }
44 }
转过来的,很好的例子,比教容易弄懂。
另外编译的时候别忘了加上-lz