1 /*
2 Copyright (C) 2021 The Blosc Developers <blosc@blosc.org>
3 https://blosc.org
4 License: BSD 3-Clause (see LICENSE.txt)
5
6 Example program demonstrating use of the Blosc filter from C code.
7
8 To compile this program:
9
10 $ gcc compress_file.c -o compress_file -lblosc2
11
12 To run:
13
14 $ ./compress_file /usr/lib/libsqlite3.dylib libsqlite3.b2frame
15 Blosc version info: 2.0.0a6.dev ($Date:: 2018-05-18 #$)
16 Compression ratio: 5.1 MB -> 3.6 MB (1.4x)
17 Compression time: 0.0185 s, 275.2 MB/s
18
19 */
20
21 #include <stdio.h>
22 #include <blosc2.h>
23
24 #define KB 1024.
25 #define MB (1024*KB)
26 #define GB (1024*MB)
27
28 #define CHUNKSIZE (1000 * 1000)
29 #define NTHREADS 4
30
31
main(int argc,char * argv[])32 int main(int argc, char* argv[]) {
33 blosc_init();
34 static int32_t data[CHUNKSIZE];
35 size_t isize;
36 int64_t nbytes, cbytes;
37 blosc_timestamp_t last, current;
38 double ttotal;
39
40 if (argc != 3) {
41 fprintf(stderr, "Usage: compress_file input_file output_file.b2frame\n");
42 return -1;
43 }
44
45 printf("Blosc version info: %s (%s)\n",
46 BLOSC_VERSION_STRING, BLOSC_VERSION_DATE);
47
48 /* Create a super-chunk container */
49 blosc2_cparams cparams = BLOSC2_CPARAMS_DEFAULTS;
50 cparams.typesize = 1;
51 cparams.compcode = BLOSC_BLOSCLZ;
52 //cparams.filters[BLOSC2_MAX_FILTERS - 1] = BLOSC_BITSHUFFLE;
53 cparams.clevel = 9;
54 cparams.nthreads = NTHREADS;
55 blosc2_dparams dparams = BLOSC2_DPARAMS_DEFAULTS;
56 dparams.nthreads = NTHREADS;
57
58 /* Create a super-chunk backed by an in-memory frame */
59 remove(argv[2]);
60 blosc2_storage storage = {.cparams=&cparams, .dparams=&dparams,
61 .contiguous=true, .urlpath=argv[2]};
62 blosc2_schunk* schunk = blosc2_schunk_new(&storage);
63
64 // Compress the file
65 blosc_set_timestamp(&last);
66 FILE* finput = fopen(argv[1], "rb");
67 if (finput == NULL) {
68 printf("Input file cannot be open.");
69 exit(1);
70 }
71 while ((isize = (int32_t)fread(data, 1, CHUNKSIZE, finput)) == CHUNKSIZE) {
72 if (blosc2_schunk_append_buffer(schunk, data, isize) < 0) {
73 fprintf(stderr, "Error in appending data to destination file");
74 return -1;
75 }
76 }
77 if (blosc2_schunk_append_buffer(schunk, data, isize) < 0) {
78 fprintf(stderr, "Error in appending data to destination file");
79 return -1;
80 }
81 fclose(finput);
82
83 /* Gather some info */
84 nbytes = schunk->nbytes;
85 cbytes = schunk->cbytes;
86 blosc_set_timestamp(¤t);
87 ttotal = blosc_elapsed_secs(last, current);
88 printf("Compression ratio: %.1f MB -> %.1f MB (%.1fx)\n",
89 nbytes / MB, cbytes / MB, (1. * nbytes) / cbytes);
90 printf("Compression time: %.3g s, %.1f MB/s\n",
91 ttotal, nbytes / (ttotal * MB));
92
93 /* Free resources */
94 blosc2_schunk_free(schunk);
95 blosc_destroy();
96 return 0;
97 }
98