easylzma C语言压缩库

程序名称:easylzma

授权协议: 未知

操作系统: 跨平台

开发语言: C/C++

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;
}

easylzma 官网

http://lloyd.github.com/easylzma/

相关编程语言

Pacman 是一个软件包管理器, 作为 ArchLinux 发行版...
Smb4K 是KDE下的网络共享浏览器 更多屏幕截图请看:...
Wine (“Wine Is Not an Emulator” 的首字母缩写)...
虚拟桌面软件,可管理最多9个虚拟桌面,你可以用热键...
UNetbootin (Universal Netboot Installer)为一种跨...
Cobbler 可以用来快速建立 Linux 网络安装环境,它已...