一 去http://www.7-zip.org/sdk.html下载源代码,目前的版本是9.20
二 把包解压之后有个C目录,把下面的Alloc.c,Alloc.h,LzFind.c,LzFind.h,LzFindMt.c,LzFindMt.h,LzHash.h,LzmaDec.c,LzmaDec.h,LzmaEnc.c,LzmaEnc.h,LzmaLib.c,LzmaLib.h,Threads.c,Threads.h,Types.h拷贝出来,这些是使用lzma算法编解码的必须文件,如果想要其他算法再拷其他文件。
三 把上述文件加入工程,并在测试文件中包含LzmaLib.h,就可以种其中的函数了
#include <iostream>
#include <fstream>
#include <time.h>
#include <windows.h>
using namespace std;
#include "lzmalib.h"
int main(int argc, char* argv[])
{
size_t insize = 0;
size_t outsize = 0;
unsigned char * inbuf = 0;
unsigned char * outbuf = 0;
unsigned char * destbuf = 0;
if(argc == 1)
{
cout<<"please input infile"<<endl;
return -1;
}
ifstream infile;
cout<<argv[1]<<endl;
infile.open (argv[1], ios::binary );
//infile.open ("stocktrace.dat", ios::binary );
// get length of file:
infile.seekg (0, ios::end);
insize = infile.tellg();
infile.seekg (0, ios::beg);
// allocate memory:
inbuf = new unsigned char [insize];
// read data as a block:
infile.read ((char *)inbuf,insize);
infile.close();
unsigned propsSize = LZMA_PROPS_SIZE;
outsize = insize;
outbuf = new unsigned char [outsize];
LARGE_INTEGER t1,t2,feq;
QueryPerformanceFrequency(&feq);//每秒跳动次数
QueryPerformanceCounter(&t1);//测前跳动次数
//unsigned long start_tick = GetTickCount();//精确到毫秒
int res = LzmaCompress(
&outbuf[LZMA_PROPS_SIZE], &outsize,
&inbuf[0], insize,
&outbuf[0], &propsSize,
0, 1<<14, -1, -1, -1, 5, -1);
//unsigned long end_tick = GetTickCount();
QueryPerformanceCounter(&t2);//测后跳动次数
double d=((double)t2.QuadPart-(double)t1.QuadPart)/((double)feq.QuadPart);//时间差秒,*1000000就是微妙
cout<<"compress time is "<< d*1000000<<endl;
ofstream outfile;
outfile.open ("lzma.test", ios::binary );
outfile.write((char *)outbuf,outsize+LZMA_PROPS_SIZE);
outfile.close();
cout<<"insize is "<<insize <<" outsize is "<<outsize+LZMA_PROPS_SIZE<<" compress ratio is "<<((float)(outsize+LZMA_PROPS_SIZE)/(float)insize)<<endl;
//cout<<"return value is "<<res<<" encode time is "<<end_tick - start_tick<<" ms"<<endl;
size_t destlen = 2*insize;
destbuf = new unsigned char [destlen];
outsize = outsize;
// end_tick = GetTickCount();
QueryPerformanceFrequency(&feq);//每秒跳动次数
QueryPerformanceCounter(&t1);//测前跳动次数
res = LzmaUncompress(&destbuf[0], &destlen, &outbuf[LZMA_PROPS_SIZE], &outsize,
&outbuf[0], LZMA_PROPS_SIZE);
QueryPerformanceCounter(&t2);//测后跳动次数
d=((double)t2.QuadPart-(double)t1.QuadPart)/((double)feq.QuadPart);//时间差秒
//unsigned long last_tick = GetTickCount();
ofstream dstfile;
dstfile.open ("lzma.org", ios::binary );
dstfile.write((char *)destbuf,destlen);
dstfile.close();
cout<<"res is "<<res<<" dest len is "<<destlen<<endl;
cout<<"decode time is "<<d*1000000<<endl;
delete [] inbuf;
delete [] outbuf;
delete [] destbuf;
//assert(propsSize == LZMA_PROPS_SIZE);
//assert(res == SZ_OK);
return 0;
}
这个事最简单的例子,另外http://asawicki.info/news_1368_lzma_sdk_-_how_to_use.html 做了比较详细的描述
本文介绍了如何使用7zip的SDK进行数据压缩和解压缩操作。首先从官方下载SDK源代码,选取必要的lzma算法相关文件并加入工程。接着在C++程序中调用LzmaCompress和LzmaUncompress函数进行压缩和解压缩,并展示了时间消耗的计算方法。最后,程序将压缩和解压缩后的数据写入文件。
7961

被折叠的 条评论
为什么被折叠?



