理解和使用zlib库

2017-12-26  本文已影响0人  微雨旧时歌丶

http://blog.csdn.net/querw/article/details/51569274


原理部分 略,详见原文

示例如下:

#include <stdio.h>
#include <string.h>
#include <assert.h>

extern "C"
{
    #include "zlib.h"
}
#pragma comment(lib, "zlib.lib")

int dump_buffer(const Bytef* buf, size_t len)
{
    for(size_t i = 0; i < len; ++i)
    {
        printf("%02x", buf[i]);
    }
    return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
    const char* inBuf = "1234,abcd,ABCD,^#@!.";
    Bytef outBuf[1024] = {0};
    Bytef restoreBuf[1024] = {0};
    int outLen = 0;
    int restoreLen = 0;
    int err = 0;
    z_stream stream;
    int fmt = 2; // 0: zlib; 1: gzip; 2: raw

    printf("source string:%s\r\n", inBuf);

    // 压缩
    stream.next_in = (z_const Bytef *)inBuf;
    stream.avail_in = (uInt)strlen(inBuf);

    stream.next_out = (Bytef *)outBuf;
    stream.avail_out = 1024;

    stream.zalloc = (alloc_func)0;
    stream.zfree = (free_func)0;
    stream.opaque = (voidpf)0;

    if(0 == fmt)
    {
        // zlib
        err = deflateInit(&stream, Z_DEFAULT_COMPRESSION);
        assert(Z_OK == err);

        err = deflate(&stream, Z_FINISH);
        assert(err == Z_STREAM_END);

        outLen = stream.total_out;

        err = deflateEnd(&stream);

        printf("zlib string(HEX):");
    }
    else if(1 == fmt)
    {
        // gzip
        err = deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY);
        assert(Z_OK == err);

        err = deflate(&stream, Z_FINISH);
        assert(err == Z_STREAM_END);

        outLen = stream.total_out;

        err = deflateEnd(&stream);

        printf("gzip string(HEX):");
    }
    else if(2 == fmt)
    {
        // raw
        err = deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS * -1, 8, Z_DEFAULT_STRATEGY);
        assert(Z_OK == err);

        err = deflate(&stream, Z_FINISH);
        assert(err == Z_STREAM_END);

        outLen = stream.total_out;

        err = deflateEnd(&stream);

        printf("raw deflate string(HEX):");
    }
    else
    {
        assert(0);
    }

    dump_buffer(outBuf, outLen);
    printf("\r\n");

    // 解压缩
    stream.next_in = (z_const Bytef *)outBuf;
    stream.avail_in = (uInt)outLen;

    stream.next_out = (Bytef *)restoreBuf;
    stream.avail_out = 1024;

    stream.zalloc = (alloc_func)0;
    stream.zfree = (free_func)0;
    stream.opaque = (voidpf)0;

    if(0 == fmt)
    {
        // zlib
        err = inflateInit(&stream);
        assert(Z_OK == err);

        err = inflate(&stream, Z_FINISH);
        assert(err == Z_STREAM_END);

        restoreLen = stream.total_out;

        err = inflateEnd(&stream);
    }
    else if(1 == fmt)
    {
        // gzip
        err = inflateInit2(&stream, MAX_WBITS + 16);
        assert(Z_OK == err);

        err = inflate(&stream, Z_FINISH);
        assert(err == Z_STREAM_END);

        restoreLen = stream.total_out;

        err = inflateEnd(&stream);
    }
    else if(2 == fmt)
    {
        // raw
        err = inflateInit2(&stream, MAX_WBITS * -1);
        assert(Z_OK == err);

        err = inflate(&stream, Z_FINISH);
        assert(err == Z_STREAM_END);

        restoreLen = stream.total_out;

        err = inflateEnd(&stream);
    }
    else
    {
        assert(0);
    }

    printf("restored string:%s\r\n", (char*)restoreBuf);

    printf("Press Enter to continue...");
    getchar();
    return err;
}

fmt 分别设置为 0, 1, 2 时的运行结果:
source string:1234,abcd,ABCD,^#@!.
zlib string(HEX):789c33343236d1494c4a4ed171747276d189537650d40300357804f3
restored string:1234,abcd,ABCD,^#@!.

source string:1234,abcd,ABCD,^#@!.
gzip string(HEX):1f8b080000000000000b33343236d1494c4a4ed171747276d189537650d4030065d6b0c314000000
restored string:1234,abcd,ABCD,^#@!.

source string:1234,abcd,ABCD,^#@!.
raw deflate string(HEX):33343236d1494c4a4ed171747276d189537650d40300
restored string:1234,abcd,ABCD,^#@!.

可以看到中间的压缩数据都是相同的,只是头尾不同.

上一篇下一篇

猜你喜欢

热点阅读