-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_gzip.cpp
104 lines (95 loc) · 2.53 KB
/
test_gzip.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdio.h>
#include<stdlib.h>
#include <string.h>
#include <zlib.h>
// gzCompress: do the compressing
int gzCompress(const char *src, int srcLen, char *dest, int destLen)
{
z_stream c_stream;
int err = 0;
int windowBits = 15;
int GZIP_ENCODING = 16;
if(src && srcLen > 0)
{
c_stream.zalloc = (alloc_func)0;
c_stream.zfree = (free_func)0;
c_stream.opaque = (voidpf)0;
if(deflateInit2(&c_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
windowBits | GZIP_ENCODING, 8, Z_DEFAULT_STRATEGY) != Z_OK) return -1;
c_stream.next_in = (Bytef *)src;
c_stream.avail_in = srcLen;
c_stream.next_out = (Bytef *)dest;
c_stream.avail_out = destLen;
while (c_stream.avail_in != 0 && c_stream.total_out < destLen)
{
if(deflate(&c_stream, Z_NO_FLUSH) != Z_OK) return -1;
}
if(c_stream.avail_in != 0) return c_stream.avail_in;
for (;;) {
if((err = deflate(&c_stream, Z_FINISH)) == Z_STREAM_END) break;
if(err != Z_OK) return -1;
}
if(deflateEnd(&c_stream) != Z_OK) return -1;
return c_stream.total_out;
}
return -1;
}
// gzDecompress: do the decompressing
int gzDecompress(const char *src, int srcLen, const char *dst, int dstLen){
z_stream strm;
strm.zalloc=NULL;
strm.zfree=NULL;
strm.opaque=NULL;
strm.avail_in = srcLen;
strm.avail_out = dstLen;
strm.next_in = (Bytef *)src;
strm.next_out = (Bytef *)dst;
int err=-1, ret=-1;
err = inflateInit2(&strm, MAX_WBITS+16);
if (err == Z_OK){
err = inflate(&strm, Z_FINISH);
if (err == Z_STREAM_END){
ret = strm.total_out;
}
else{
inflateEnd(&strm);
return err;
}
}
else{
inflateEnd(&strm);
return err;
}
inflateEnd(&strm);
return err;
}
int main()
{
char* src = (char*)"just for test, dd, dd, dd";
int size_src = strlen(src);
char* compressed = (char*)malloc(size_src*2);
memset(compressed, 0, size_src*2);
printf("to compress src: %s\n", src);
printf("to compress src size: %d\n", size_src);
int gzSize = gzCompress(src, size_src, compressed, size_src*2);
if (gzSize <= 0)
{
printf("compress error.\n");
return -1;
}
printf("compressed: ");
int i = 0;
for (; i<gzSize; ++i)
{
printf("%02x ", compressed[i]);
}
printf("\ncompressed size: %d\n", gzSize);
char* uncompressed = (char*)malloc(size_src*2);
memset(uncompressed, 0, size_src*2);
int ret = gzDecompress(compressed, gzSize, uncompressed, size_src*2);
printf("uncompressed: %s\n", uncompressed);
printf("uncompressed size: %d\n", strlen(uncompressed));
free(compressed);
free(uncompressed);
return 0;
}