From c17b523d4710134218bca4f2fdbe8fd0cee39ecd Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sun, 12 Jul 2026 15:19:57 +0200 Subject: Erstes Beispiel für die Benutzung von zlib MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/simple.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/simple.c (limited to 'src/simple.c') diff --git a/src/simple.c b/src/simple.c new file mode 100644 index 0000000..5209841 --- /dev/null +++ b/src/simple.c @@ -0,0 +1,77 @@ +// Standard C +#include +#include + +// System +#include + +// zlib +#include + +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; +} -- cgit v1.3