:: bzip3 / examples / hl-api.c 1.1 KB raw

1
2
#include <libbz3.h>
3
#include <stdio.h>
4
#include <stdlib.h>
5
6
#define MB (1024 * 1024)
7
8
int main(void) {
9
    printf("Compressing shakespeare.txt back and forth in memory.\n");
10
11
    // Read the entire "shakespeare.txt" file to memory:
12
    FILE * fp = fopen("shakespeare.txt", "rb");
13
    fseek(fp, 0, SEEK_END);
14
    size_t size = ftell(fp);
15
    fseek(fp, 0, SEEK_SET);
16
    char * buffer = malloc(size);
17
    fread(buffer, 1, size, fp);
18
    fclose(fp);
19
20
    // Compress the file:
21
    size_t out_size = bz3_bound(size);
22
    char * outbuf = malloc(out_size);
23
    int bzerr = bz3_compress(1 * MB, buffer, outbuf, size, &out_size);
24
    if (bzerr != BZ3_OK) {
25
        printf("bz3_compress() failed with error code %d", bzerr);
26
        return 1;
27
    }
28
29
    printf("%d => %d\n", size, out_size);
30
31
    // Decompress the file.
32
    bzerr = bz3_decompress(outbuf, buffer, out_size, &size);
33
    if (bzerr != BZ3_OK) {
34
        printf("bz3_decompress() failed with error code %d", bzerr);
35
        return 1;
36
    }
37
38
    printf("%d => %d\n", out_size, size);
39
40
    free(buffer);
41
    free(outbuf);
42
    return 0;
43
}
tab: 248 wrap: offon