C语言压缩与解压缩字符串(可用于进程间通信)

#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <zlib.h>


#define dPrint(fmt, ...) do{fprintf(stderr, "[%s:%d] " fmt "\r\n", __FUNCTION__, __LINE__, ##__VA_ARGS__);}while(0)
#define HexPrint(_buf, _len) \
{\
    int _m_i = 0;\
    char *_m_buf = (char *)(_buf);\
    int _m_len = (int)(_len);\
    printf("[%s:%d] \r\n", __FUNCTION__, __LINE__);\
    printf("*****************************\n");\
    for(_m_i = 0; _m_i < _m_len; _m_i++)\
    {\
        printf("\033[32m%02x \033[0m", _m_buf[_m_i] & 0xff);\
        if(!((_m_i+1) % 10)) printf("\n");\
    }\
    printf("\nsize = %d\n*****************************\n", _m_len);\
}


int data_compress(char *idata, int ilen, char *odata, int *olen)
{
    z_stream z = {0};


    z.next_in = idata;
    z.avail_in = ilen;
    z.next_out = odata;
    z.avail_out = *olen;
    /* 使用最高压缩比 */
    if (deflateInit(&z, Z_BEST_COMPRESSION) != Z_OK) {
        printf("deflateInit failed!\n");
        return -1;
    }


    if (deflate(&z, Z_NO_FLUSH) != Z_OK) {
        printf("deflate Z_NO_FLUSH failed!\n");
        return -1;
    }


    if (deflate(&z, Z_FINISH) != Z_STREAM_END) {
        printf("deflate Z_FINISH failed!\n");
        return -1;
    }


    if (deflateEnd(&z) != Z_OK) {
        printf("deflateEnd failed!\n");
        return -1;
    }
    *olen = *olen - z.avail_out;


    return 0;
}




int data_decompress(char *idata, int ilen, char *odata, int *olen)
{
    z_stream z = {0};


    z.next_in = idata;
    z.avail_in = ilen;
    z.next_out = odata;
    z.avail_out = *olen;


    if (inflateInit(&z) != Z_OK) {
        printf("inflateInit failed!\n");
        return -1;
    }


    if (inflate(&z, Z_NO_FLUSH) != Z_STREAM_END) {
        printf("inflate Z_NO_FLUSH failed!\n");
        return -1;
    }


    if (inflate(&z, Z_FINISH) != Z_STREAM_END) {
        printf("inflate Z_FINISH failed!\n");
        return -1;
    }


    if (inflateEnd(&z) != Z_OK) {
        printf("inflateEnd failed!\n");
        return -1;
    }


    *olen = *olen - z.avail_out;


    return 0;
}


//char data[] = "\nYouth\n\nYouth is not a time of life; it is a state of mind; it is not a matter of rosy cheeks, red lips and supple knees; it is a matter of the will, a quality of the imagination, a vigor of the emotions; it is the freshness of the deep springs of life.\n\nYouth means a temperamental predominance of courage over timidity, of the appetite for adventure over the love of ease. This often exists in a man of 60 more than a boy of 20. Nobody grows old merely by a number of years. We grow old by deserting our ideals.\n\nYears may wrinkle the skin, but to give up enthusiasm wrinkles the soul. Worry, fear, self-distrust bows the heart and turns the spirit back to dust.\n\nWhether 60 or 16, there is in every human being’s heart the lure of wonders, the unfailing appetite for what’s next and the joy of the game of living. In the center of your heart and my heart, there is a wireless station; so long as it receives messages of beauty, hope, courage and power from man and from the infinite, so long as you are young.\n\nWhen your aerials are down, and your spirit is covered with snows of cynicism and the ice of pessimism, then you’ve grown old, even at 20; but as long as your aerials are up, to catch waves of optimism, there’s hope you may die young at 80.";
char data[] = "aaaaaaaabbbbbbbbbbbbbbccccccccccccccc";
int main(int argc, char **argv)
{
    int compress_len, decompress_len;
//    unsigned char data[1024] = {0};
    unsigned char compress_buf[2048] = {0};
    unsigned char decompress_buf[4096] = {0};


//    memset(data, 0x00, sizeof(data));
    memset(compress_buf, 0x00, sizeof(compress_buf));
    memset(decompress_buf, 0x00, sizeof(decompress_buf));
    
//    data[0] = 0x01;
//    data[777] = 0x02;    


    dPrint("... origin data ...");
    HexPrint(data, sizeof(data));
    
    /* 压缩数据 */
    compress_len = sizeof(compress_buf);
    if (data_compress(data, sizeof(data),
                      compress_buf, &compress_len) < 0) {
        return -1;
    }


    dPrint("... compress data ...");
    HexPrint(compress_buf, compress_len);


    /* 解压缩数据 */
    decompress_len = sizeof(decompress_buf);
    if (data_decompress(compress_buf, compress_len,
                        decompress_buf, &decompress_len) < 0) {
        return -1;
    }


    dPrint("... decompress data ...");
    HexPrint(decompress_buf, decompress_len);                          
        
    return 0;
}

缺点:占用太多cpu,在通信频繁时容易造成拥塞。

使用g++编译时,因为编译器对指针要求偏高,所一需要将指针类型强转。

更多推荐

C语言压缩与解压缩字符串(可用于进程间通信)