使用 redis 中的 lzf 压缩算法

lzfP.h:对 lzf.h 和 lzfP.h 的合并
lzfP.cpp:对 lzf_c.c 和 lzf_d.c 的合并
lzf.h:https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/lzf.h
lzfP.h:https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/lzfP.h
lzf_c.c:https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/lzf_c.h
lzf_d.c:https://github.com/huangz1990/redis-3.0-annotated/blob/unstable/src/lzf_d.h

#include #include #include "lzfP.h" using namespace std; int main() { string value = "https://www.it610.com/article/hello world hello world hello world hello world"; size_t len = value.size(); // 字符串未压缩前的长度 cout << "压缩前:val = " << value << endl; cout << "压缩前:len = " << len << endl << endl; // -------------------压缩--------------------------------- size_t comprlen; // 压缩后的长度 size_t outlen; // 输出缓存的最大长度 unsigned char byte; int n, nwritten = 0; void *out; /* We require at least four bytes compression for this to be worth it */ if (len <= 4) { cout << "len <= 4" << endl; return 0; } outlen = len-4; if ((out = malloc(outlen+1)) == NULL) { cout << "out = malloc(outlen+1)" << endl; return 0; } comprlen = lzf_compress(value.data(), len, out, outlen); if (comprlen == 0) { cout << "outlen == " << outlen << endl; cout << "comprlen == 0" << endl; free(out); return 0; } cout << "压缩后:val = " << out << endl; cout << "压缩后:len = " << comprlen << endl << endl; // -------------------解压缩--------------------------------- char*val = NULL; // 字符串空间 if ((val = (char*)malloc(len)) == NULL) { cout << "lzf_decompress" << endl; return 0; }// 解压,得出字符串 if (lzf_decompress(out, comprlen, val, len) == 0) { cout << "lzf_decompress" << endl; return 0; } cout << "解压后:val = " << val << endl; cout << "解压后:len = " << len << endl; free(out); free(val); getchar(); getchar(); getchar(); getchar(); return 0; }

【使用 redis 中的 lzf 压缩算法】使用 redis 中的 lzf 压缩算法
文章图片

    推荐阅读