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
|
// Standard C
#include <stdio.h>
#include <stdlib.h>
// System
#include <unistd.h>
// zlib
#include <zlib.h>
static void
usage(void)
{
(void) fprintf(stderr, "compress [-d] infile outfile\n");
exit(EXIT_FAILURE);
}
int
main(int argc, char *argv[])
{
// int compress = 1;
int level = 5;
int opt = 0;
while ( (opt = getopt(argc, argv, "dl")) != -1 ) {
switch ( opt ) {
case 'd':
// compress = 0; // decompress
break;
case 'l':
level = 9;
break;
case '?':
default:
usage();
break;
}
}
argc -= optind;
argv += optind;
if ( argc != 2 ) {
usage();
}
FILE *in_file = fopen(argv[0], "rb");
FILE *out_file = fopen(argv[1], "wb");
(void) fseek(in_file, 0, SEEK_END);
unsigned long insize = ftell(in_file); // max 2GB...
(void) fseek(in_file, 0, SEEK_SET);
unsigned char *indata = malloc(insize);
(void) fread(indata, 1, insize, in_file);
unsigned long outsize = compressBound(insize);
unsigned char *outdata = malloc(outsize);
int res = compress2(outdata, &outsize, indata, insize, level);
if ( res == Z_OK ) {
(void) fwrite(outdata, 1, outsize, out_file);
}
else {
printf("Error: %d\n", res);
}
free(outdata);
free(indata);
(void) fclose(out_file);
(void) fclose(in_file);
return EXIT_SUCCESS;
}
|