easylzma 介绍
Easylzma 是一个实现了 LZMA 压缩和解压缩算法的 C 语言库。
LZMA ,( _Lempel -Ziv-Markov chain-Algorithm_的縮寫),是
2001年以來得到發展的一個數據壓縮演算法,它用於 7-Zip 歸檔工具中的 7z 格式。它使用類似於 LZ77 的字典編碼機制,在一般的情況下壓縮率比
bzip2 為高,用於壓縮的字典檔大小可達4GB。
示例代码:
/
an example of basic LZMA compression to and from memory buffers
using the easylzma library.
/
include “easylzma/compress.h”
include
include
struct dataStream
{
const unsigned char * inData;
size_t inLen;
unsigned char * outData;
size_t outLen;
};
/ an input callback that will be passed to elzma_compress_run(),
it reads from a memory buffer /
static int
inputCallback(void ctx, void buf, size_t * size)
{
size_t rd = 0;
struct dataStream * ds = (struct dataStream ) ctx;
assert(ds != NULL);
rd = (ds->inLen < size) ? ds->inLen : size;
if (rd > 0) {
memcpy(buf, (void *) ds->inData, rd);
ds->inData += rd;
ds->inLen -= rd;
}
*size = rd;
return 0;
}
/ an ouput callback that will be passed to elzma_compress_run(),
it reallocs and writes to a memory buffer /
static size_t
outputCallback(void ctx, const void buf, size_t size)
{
struct dataStream * ds = (struct dataStream ) ctx;
assert(ds != NULL);
if (size > 0) {
ds->outData = realloc(ds->outData, ds->outLen + size);
memcpy((void *) (ds->outData + ds->outLen), buf, size);
ds->outLen += size;
}
return size;
}
/ a function that will compress data using a 1mb dictionary and a
client specified encoding format (one of ELZMA_lzip or ELZMA_lzma) /
int
simpleCompress(elzma_file_format format, const unsigned char * inData,
size_t inLen, unsigned char * outData,
size_t * outLen)
{
int rc;
elzma_compress_handle hand;
/ allocate compression handle /
hand = elzma_compress_alloc();
assert(hand != NULL);
/ configure the compression run with mostly default parameters /
rc = elzma_compress_config(hand, ELZMA_LC_DEFAULT,
ELZMA_LP_DEFAULT, ELZMA_PB_DEFAULT,
5, (1 << 20) / 1mb /,
format, inLen);
/ fail if we couldn’t allocate /
if (rc != ELZMA_E_OK) {
elzma_compress_free(&hand);
return rc;
}
/ now run the compression /
{
/ set up the context structure that will be passed to
stream callbacks */
struct dataStream ds;
ds.inData = inData;
ds.inLen = inLen;
ds.outData = NULL;
ds.outLen = 0;
/ run the streaming compression /
rc = elzma_compress_run(hand, inputCallback, (void ) &ds,
outputCallback, (void ) &ds);
if (rc != ELZMA_E_OK) {
if (ds.outData != NULL) free(ds.outData);
elzma_compress_free(&hand);
return rc;
}
outData = ds.outData;
outLen = ds.outLen;
}
return rc;
}