deflate and inflate (zlib.h) in C

后端 未结 3 636
南方客
南方客 2020-12-28 09:53

I am trying to implement the zlib.h deflate and inflate functions to compress and decompress a char array (not a file).

I would like to know if the following syntax

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-28 10:49

    zlib already has a simple inflate/deflate function you can use.

    char a[50] = "Hello, world!";
    char b[50];
    char c[50];
    
    uLong ucompSize = strlen(a)+1; // "Hello, world!" + NULL delimiter.
    uLong compSize = compressBound(ucompSize);
    
    // Deflate
    compress((Bytef *)b, &compSize, (Bytef *)a, ucompSize);
    
    // Inflate
    uncompress((Bytef *)c, &ucompSize, (Bytef *)b, compSize);
    

    When in doubt, check out the zlib manual. My code is crappy, sorry =/

提交回复
热议问题