2 * Compresses data to the DEFLATE format.
3 * The compressed data is ready to use with inflatemem().
5 * gcc -O2 -o deflater deflater.c -lz
7 * Author: Piotr Fusik <fox@scene.pl>
13 #define IN_SIZE_MAX 60000U
14 #define OUT_SIZE_MAX 60000U
16 int main(int argc, char* argv[])
25 /* check command line */
28 "Compresses a file to the DEFLATE format.\n"
29 "24 Aug 2002, Piotr Fusik <fox@scene.pl>\n"
30 "Usage: deflater input_file deflated_file\n"
36 inbuf = malloc(IN_SIZE_MAX);
37 outbuf = malloc(OUT_SIZE_MAX);
38 if (inbuf == NULL || outbuf == NULL) {
39 fprintf(stderr, "deflater: Out of memory!\n");
44 fp = fopen(argv[1], "rb");
49 inlen = fread(inbuf, 1, IN_SIZE_MAX, fp);
53 stream.next_in = inbuf;
54 stream.avail_in = inlen;
55 stream.next_out = outbuf;
56 stream.avail_out = OUT_SIZE_MAX;
57 stream.zalloc = (alloc_func) 0;
58 stream.zfree = (free_func) 0;
59 if (deflateInit2(&stream, Z_BEST_COMPRESSION, Z_DEFLATED,
60 -MAX_WBITS, 9, Z_DEFAULT_STRATEGY) != Z_OK) {
61 fprintf(stderr, "deflater: deflateInit2 failed\n");
64 if (deflate(&stream, Z_FINISH) != Z_STREAM_END) {
65 fprintf(stderr, "deflater: deflate failed\n");
68 if (deflateEnd(&stream) != Z_OK) {
69 fprintf(stderr, "deflater: deflateEnd failed\n");
74 fp = fopen(argv[2], "wb");
79 outlen = fwrite(outbuf, 1, stream.total_out, fp);
81 if (outlen != stream.total_out) {
87 printf("Compressed %s (%d bytes) to %s (%d bytes)\n",
88 argv[1], inlen, argv[2], outlen);