File: gzip_write_stream.c

package info (click to toggle)
libslow5lib 0.7.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 25,092 kB
  • sloc: ansic: 11,825; python: 1,179; sh: 547; makefile: 91; cpp: 40
file content (51 lines) | stat: -rw-r--r-- 1,056 bytes parent folder | download | duplicates (3)
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
#include <zlib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void) {

    int ret;
    z_stream strm;

    char inp[] = "Hello, world!\n";
    uInt inp_sz = strlen(inp) + 1; // +1 for '\0'

    strm.zalloc = Z_NULL;
    strm.zfree = Z_NULL;
    strm.opaque = Z_NULL;

    strm.avail_in = inp_sz;
    strm.next_in = inp;

    ret = deflateInit2(&strm, 
            Z_DEFAULT_COMPRESSION, 
            Z_DEFLATED,
            MAX_WBITS | 16, // Gzip compatible compression
            8, // Default memory
            Z_DEFAULT_STRATEGY);

    if (ret != Z_OK) {
        fprintf(stderr, "failed: deflateInit2\n");
        return EXIT_FAILURE;
    }

    uLong out_sz = deflateBound(&strm, inp_sz);
    char out[out_sz];

    strm.avail_out = out_sz;
    strm.next_out = out;

    ret = deflate(&strm, Z_FINISH);

    if (ret != Z_STREAM_END) {
        fprintf(stderr, "failed: deflate\n");
        return EXIT_FAILURE;
    }

    fwrite(out, sizeof *out, strm.total_out, stdout);

    deflateEnd(&strm);

    return EXIT_SUCCESS;
}