http://www.7-zip.org/zh-cn/sdk.html
LZMA 是 7-Zip 程序中 7z 格式 的默认压缩算法。LZMA 能提供给用户极高的压缩比及较快的压缩速度,它非常适合与应用程序集成。
LZMA 软件开发工具包 (以下简称 SDK) 给开发客户提供文档、源代码以及几个使用 LZMA 压缩算法制作的应用程序的例子。我下载到的文件名是lzma457.tar.bz2。
假设压缩包解压在C:\lzma457\下,则用vc(我用的是vc2005)打开位于C:\lzma457\CPP\7zip\Compress\LZMA_Alone\下的工程文件,编译即可得到可执行文件lzma.exe,提醒一下,该文件的默认路径在C:\util。在linux下,则只需要到相应LZMA_Alone目录下执行make –f makefile.gcc即可得到可执行文件lzma。
该程序包括压缩和解压两部分功能。简单说明为:
Usage: LZMA <e|d> inputFile outputFile
e: encode file
d: decode file
下面讨论如何将解压代码集成到自己的程序中,最简单的方式是使用C:\lzma457\C\Compress\Lzma目录下的三个文件:
LzmaDecode.c
LzmaDecode.h
LzmaTypes.h
该目录下的LzmaTest.c演示了如何使用解压函数。可以跟一下这两个c文件编译链接后得到的可执行码。
以下是一个使用解压函数的简单例子(不支持超过4G数据量的情况):
const unsigned char* originalStream是待解压数据
uint32 size是待解压数据的大小(in byte)
/* Read LZMA properties for compressed stream */
const unsigned char* properties = originalStream;
/* Decode LZMA properties and allocate memory */
CLzmaDecoderState state;
LzmaDecodeProperties(&state.Properties, properties, LZMA_PROPERTIES_SIZE);
state.Probs = (CProb *)malloc(LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
/* Read uncompressed size */
uint32 outSize = 0;
for (int i = 0; i < 8; i++)
{
unsigned char b = originalStream [LZMA_PROPERTIES_SIZE + i];
outSize += (b) << (i * 8);
}
unsigned char *outStream = (unsigned char *)malloc(outSize);
uint32 compressedSize = size - (LZMA_PROPERTIES_SIZE + 8);
const unsigned char *inStream = &( originalStream[LZMA_PROPERTIES_SIZE + 8]);
uint32 inProcessed = 0;
uint32 outProcessed = 0;
int res = LzmaDecode(&state, inStream, compressedSize, &inProcessed, outStream, outSize, &outProcessed);