7zip 学习笔记
7zip 学习笔记1
7zip 说明:
关于此库的详细说明请看 http://zh.wikipedia.org/wiki/Zlib 或自己百度。
中文许可协议:
http://7z.sparanoid.com/license.txt
7zip 获取:
http://sourceforge.net/projects/sevenzip/ 下载最新版
http://nchc.dl.sourceforge.net/project/sevenzip/7-Zip/9.07%20beta/7z907.tar.bz2
http://nchc.dl.sourceforge.net/project/sevenzip/7-Zip/9.07%20beta/lzma907.tar.bz2
7zip 编译:
我们选择VS2010开发环境。
解压7zip包,四个目录ASM,C,CPP,DOC。我们进入CPP目录下,进入命令行。进入7zip目录,先运行VS2008的环境设置脚本。vsvars32.bat,VS2010的TOOLS目录下。
然后敲入nmake,编译开始了,编译时间为10分钟左右!!!(我编译的是整个工程,包括7ZIP的窗口程序和命令行,我当初不知道)喝杯茶,上个厕所就好了^_^
编译完后中间文件多达300多兆。我们运行清理脚本:
------------保存以下脚本为BAT文件---------------
@echo Off
@echo 正在删除中间文件......
del /s /a *.obj *.ncb BuildLog.htm *.user *.exp *.idb *.suo *.res *.dep *.pdb *.netmodule *.aps *.ilk 2>nul
@echo 删除工作完成了!
pause
------------------------------------------------
将虚线部分代码保存为BAT文件,谢谢CPP目录运行之,片刻之后垃圾就清除了。
7zip 使用:
在开始使用库之前先来用下7zip为我们提供的压缩算法:7z/Lzma
在C目录中有算法文件,进入Util\LzmaLib目录,编译生成LIB库,导出了以下两函数:
LzmaCompress(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t srcLen,
unsigned char *outProps, size_t *outPropsSize,
int level,
unsigned dictSize,
int lc,
int lp,
int pb,
int fb,
int numThreads
);
MY_STDAPI LzmaUncompress(unsigned char *dest, size_t *destLen, const unsigned char *src, SizeT *srcLen,
const unsigned char *props, size_t propsSize);
LzmaCompress 为压缩函数,LzmaUncompress 为解压缩函数:
导入Types.h 和 Lzmalib.h 到工程中。
代码如下:
// LzmaTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "LzmaLib.h"
extern "C"
{
#pragma comment(lib,"lzma.lib")
};
int _tmain(int argc, _TCHAR* argv[])
{
FILE *pFile;
_tfopen_s(&pFile,_T("file.dat"),_T("rb"));
if (pFile == NULL)
{
_ftprintf_s(stderr,_T("Error to Open the file!"));
return -1;
}
fseek(pFile,0,SEEK_END);
size_t srcLen = ftell(pFile);
rewind(pFile);
size_t destLen = srcLen*2;
unsigned char *psrcRead = new unsigned char[srcLen]; //原始文件数据
unsigned char *pDecomress = new unsigned char[srcLen]; //存放解压缩数据
unsigned char *pLzma = new unsigned char[destLen]; //存放压缩数据
fread(psrcRead,sizeof(char),srcLen,pFile);
unsigned char prop[5];
size_t sizeProp = 5;
if (SZ_OK != LzmaCompress(pLzma,&destLen,psrcRead,srcLen,prop,&sizeProp,5,(1<<24),3,0,2,32,2))
{//出错了
_ftprintf_s(stderr,_T("压缩时出错!"));
delete psrcRead;
delete pDecomress;
delete pLzma;
fclose(pFile);
return -1;
}
FILE *pCompressFile;
_tfopen_s(&pCompressFile,_T("compress.dat"),_T("wb")); //写入压缩后的数据
if (pCompressFile == NULL)
{
_ftprintf_s(stderr,_T("创建文件出错!"));
delete psrcRead;
delete pDecomress;
delete pLzma;
fclose(pFile);
return -1;
}
fwrite(pLzma,sizeof(char),destLen,pCompressFile);
fclose(pCompressFile);
FILE *pDecompressFile;
_tfopen_s(&pDecompressFile,_T("decompress.dat"),_T("wb")); //写入解压缩数据
if (pDecompressFile == NULL)
{
_ftprintf_s(stderr,_T("写入数据出错!"));
delete psrcRead;
delete pDecomress;
delete pLzma;
fclose(pFile);
return -1;
}
if (SZ_OK != LzmaUncompress(pDecomress,&srcLen,pLzma,&destLen,prop,5))
{
delete psrcRead;
delete pDecomress;
delete pLzma;
fclose(pDecompressFile);
fclose(pFile);
return -1;
}
fwrite(pDecomress,sizeof(char),srcLen,pDecompressFile);
delete psrcRead;
delete pDecomress;
delete pLzma;
fclose(pDecompressFile);
fclose(pFile);
return 0;
}
编译好后,放入一个文件到当前目录,我放了个DLL,改名为FILE.DAT,运行之,一个15兆的文件就只有2兆了!!