-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompressor.c
67 lines (59 loc) · 1.8 KB
/
compressor.c
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
#include "compressor.h"
int compress_file(const char *source, const char *dest) {
FILE *src_file = fopen(source, "rb");
if (!src_file) {
fprintf(stderr, "Error: Cannot open source file %s\n", source);
return -1;
}
FILE *dest_file = fopen(dest, "wb");
if (!dest_file) {
fclose(src_file);
fprintf(stderr, "Error: Cannot create destination file %s\n", dest);
return -1;
}
int ret;
z_stream strm;
unsigned char in[CHUNK];
unsigned char out[CHUNK];
// Initialize zlib stream
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION);
if (ret != Z_OK) {
fclose(src_file);
fclose(dest_file);
fprintf(stderr, "\033[1;31mError: deflateInit failed\033[0m\n");
return ret;
}
// compress the file
do {
strm.avail_in = fread(in, 1, CHUNK, src_file);
if (ferror(src_file)) {
deflateEnd(&strm);
fclose(src_file);
fclose(dest_file);
fprintf(stderr, "\033[1;31mError: Reading source file\033[0m\n");
return -1;
}
strm.next_in = in;
do {
strm.avail_out = CHUNK;
strm.next_out = out;
ret = deflate(&strm, Z_FINISH);
if (ret == Z_STREAM_ERROR) {
deflateEnd(&strm);
fclose(src_file);
fclose(dest_file);
fprintf(stderr, "\033[1;31mError: deflate error\033[0m\n");
return -1;
}
fwrite(out, 1, CHUNK - strm.avail_out, dest_file);
} while (strm.avail_out == 0);
} while (ret != Z_STREAM_END);
// clean up
deflateEnd(&strm);
fclose(src_file);
fclose(dest_file);
return 0;
}