LZO算法简介
LZO算法说明
LZO 是一个用 ANSI C 语言编写的无损压缩库。他能够提供非常快速的压缩和解压功能。解压并不需要内存的支持。即使使用非常大的压缩比例进行缓慢压缩出的数据,依然能够非常快速的解压。LZO 遵循 GNU 的 GPL 使用许可。
介绍
LZO 非常适合进行数据的实时压缩解压处理,这就是说他更关心操作速度,而不是压缩比例。
LZO 使用 ANSI C 语言编写,并且压缩后的数据也被设计为可以跨平台使用的格式。
LZO 拥有如下的特点:
解压速度很快,并且很简单;
解压时不需要内存支持;
压缩的速度还不错;
压缩时只需要 64 KiB 的内存支持;
压缩比例可以根据需要调节,而这并不影响解压的效率,提高压缩比例自然会降低压缩速度;
压缩包含了很多的压缩级别,提供很多选择;
提供只需要 8 KiB 内存支持的压缩级别;
提供线程安全;
提供无损压缩;
LZO提供多重压缩,和复原解压;
LZO算法的编解码器
先下载好lzo-2.10,目录如下所示:
- 列表内容
lzo-2.10
asm
autoconf
doc
examples
include
lzotest
minilzo
src
tests
util
其中,lzoconf.h是LZO数据压缩库的配置,lzodefs.h是对体系结构、操作系统和编译器特定的定义,minilzo.h是迷你LZO实时数据压缩库的子集。minilzo.c是C语言版本的迷你LZO实时数据压缩库的子集,testmini.c是minilzo的测试文件。
一、选择待压缩文件按钮
void CcompressDlg::OnBnClickedButton3()
{
// TODO: 在此添加控件通知处理程序代码
CString strFile = _T("");
CFileDialog dlgFile(TRUE, NULL, NULL, OFN_HIDEREADONLY, _T("Describe Files (*.cfg)|*.cfg|All Files (*.*)|*.*||"), NULL);
if (dlgFile.DoModal())
{
strFile = dlgFile.GetPathName();
}
UpdateData(true); // 获取数据
path = strFile;
UpdateData(false); // 更新数据
}
void CcompressDlg::OnBnClickedButton3()
{
// TODO: 在此添加控件通知处理程序代码
CString strFile = _T("");
CFileDialog dlgFile(TRUE, NULL, NULL, OFN_HIDEREADONLY, _T("Describe Files (*.cfg)|*.cfg|All Files (*.*)|*.*||"), NULL);
if (dlgFile.DoModal())
{
strFile = dlgFile.GetPathName();
}
UpdateData(true); // 获取数据
path = strFile;
UpdateData(false); // 更新数据
}
二、选择解压缩文件按钮
void CcompressDlg::OnBnClickedButton4()
{
// TODO: 在此添加控件通知处理程序代码
CString strFile = _T("");
CFileDialog dlgFile(TRUE, NULL, NULL, OFN_HIDEREADONLY, _T("Describe Files (*.cfg)|*.cfg|All Files (*.*)|*.*||"), NULL);
if (dlgFile.DoModal())
{
strFile = dlgFile.GetPathName();
}
UpdateData(true); // 获取数据
path_decompress = strFile;
UpdateData(false); // 更新数据
}
void CcompressDlg::OnBnClickedButton4()
{
// TODO: 在此添加控件通知处理程序代码
CString strFile = _T("");
CFileDialog dlgFile(TRUE, NULL, NULL, OFN_HIDEREADONLY, _T("Describe Files (*.cfg)|*.cfg|All Files (*.*)|*.*||"), NULL);
if (dlgFile.DoModal())
{
strFile = dlgFile.GetPathName();
}
UpdateData(true); // 获取数据
path_decompress = strFile;
UpdateData(false); // 更新数据
}
三、压缩按钮
void CcompressDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
#ifdef _UNICODE
LONG len1;
len1 = WideCharToMultiByte(CP_ACP, 0, path, -1, NULL, 0, NULL, NULL);
char *textPath = (char *)malloc(sizeof(char)*(len1 + 1));
memset(textPath, 0, len1 + 1);
WideCharToMultiByte(CP_ACP, 0, path, -1, textPath, len1 + 1, NULL, NULL);
#else
ptr = new char[str.GetAllocLength() + 1];
#endif
err = fopen_s(&ifp, textPath, "rb");
if (err == 0)
{
printf("Thefile'crt_fopen_s.c'wasopened\n");
}
fseek(ifp, 0, SEEK_END);
unsigned long len = ftell(ifp);
myInLen = len;
fseek(ifp, 0, SEEK_SET);
cpTemp = (char*)malloc(len * sizeof(char));
fread(cpTemp, sizeof(char), len, ifp);
char *in = cpTemp;
char *out = (char*)malloc((len + len / 16 + 64 + 3) * sizeof(char));
if (lzo_init() != LZO_E_OK)
{
printf("internal error - lzo_init() failed !!!\n");
printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable '-DLZO_DEBUG' for diagnostics)\n");
}
lzo_uint in_len;
in_len = len;
lzo_memset(in, 0, in_len);
lzo_uint out_len;
int r;
r = lzo1x_1_compress((unsigned char*)in, in_len, (unsigned char*)out, &out_len, wrkmem);
char mess[256];
if (r == LZO_E_OK)
{
sprintf_s(mess, sizeof(mess), "压缩成功!\n\n原文件大小:%lu bytes\n压缩后大小:%lu bytes\n\n解压得到的文件“compress.txt”保存于工程目录下",
(unsigned long)in_len, (unsigned long)out_len);
MessageBox(CString(mess));
}
else
{
/* this should NEVER happen */
sprintf_s(mess, sizeof(mess), "压缩失败!\n %d\n", r);
MessageBox(CString(mess));
}
/* check for an incompressible block */
if (out_len >= in_len)
{
sprintf_s(mess, sizeof(mess), "未压缩!\n");
MessageBox(CString(mess));
}
err = fopen_s(&ofp, ".\\compress.txt", "wb");
fwrite(out, sizeof(char), out_len, ofp);
fclose(ifp);
fclose(ofp);
free(cpTemp);
free(out);
free(textPath);
}
void CcompressDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
#ifdef _UNICODE
LONG len1;
len1 = WideCharToMultiByte(CP_ACP, 0, path, -1, NULL, 0, NULL, NULL);
char *textPath = (char *)malloc(sizeof(char)*(len1 + 1));
memset(textPath, 0, len1 + 1);
WideCharToMultiByte(CP_ACP, 0, path, -1, textPath, len1 + 1, NULL, NULL);
#else
ptr = new char[str.GetAllocLength() + 1];
#endif
err = fopen_s(&ifp, textPath, "rb");
if (err == 0)
{
printf("Thefile'crt_fopen_s.c'wasopened\n");
}
fseek(ifp, 0, SEEK_END);
unsigned long len = ftell(ifp);
myInLen = len;
fseek(ifp, 0, SEEK_SET);
cpTemp = (char*)malloc(len * sizeof(char));
fread(cpTemp, sizeof(char), len, ifp);
char *in = cpTemp;
char *out = (char*)malloc((len + len / 16 + 64 + 3) * sizeof(char));
if (lzo_init() != LZO_E_OK)
{
printf("internal error - lzo_init() failed !!!\n");
printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable '-DLZO_DEBUG' for diagnostics)\n");
}
lzo_uint in_len;
in_len = len;
lzo_memset(in, 0, in_len);
lzo_uint out_len;
int r;
r = lzo1x_1_compress((unsigned char*)in, in_len, (unsigned char*)out, &out_len, wrkmem);
char mess[256];
if (r == LZO_E_OK)
{
sprintf_s(mess, sizeof(mess), "压缩成功!\n\n原文件大小:%lu bytes\n压缩后大小:%lu bytes\n\n解压得到的文件“compress.txt”保存于工程目录下",
(unsigned long)in_len, (unsigned long)out_len);
MessageBox(CString(mess));
}
else
{
/* this should NEVER happen */
sprintf_s(mess, sizeof(mess), "压缩失败!\n %d\n", r);
MessageBox(CString(mess));
}
/* check for an incompressible block */
if (out_len >= in_len)
{
sprintf_s(mess, sizeof(mess), "未压缩!\n");
MessageBox(CString(mess));
}
err = fopen_s(&ofp, ".\\compress.txt", "wb");
fwrite(out, sizeof(char), out_len, ofp);
fclose(ifp);
fclose(ofp);
free(cpTemp);
free(out);
free(textPath);
}
四、解压缩按钮
void CcompressDlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
#ifdef _UNICODE
LONG len1;
len1 = WideCharToMultiByte(CP_ACP, 0, path_decompress, -1, NULL, 0, NULL, NULL);
char *textPath = (char *)malloc(sizeof(char)*(len1 + 1));
memset(textPath, 0, len1 + 1);
WideCharToMultiByte(CP_ACP, 0, path_decompress, -1, textPath, len1 + 1, NULL, NULL);
#else
ptr = new char[str.GetAllocLength() + 1];
#endif
err = fopen_s(&ifp, textPath, "rb");
if (err == 0)
{
printf("Thefile'crt_fopen_s.c'was opened\n");
}
fseek(ifp, 0, SEEK_END);
unsigned long len = ftell(ifp);
fseek(ifp, 0, SEEK_SET);
cpTemp = (char*)malloc(len * sizeof(char));
fread(cpTemp, sizeof(char), len, ifp);
char *in = cpTemp;
char *out = (char *)malloc(len*100* sizeof(char));
memset(out, 0, len * 100 * sizeof(char));
int r;
lzo_uint new_len;
r = lzo1x_decompress((unsigned char*)in, len, (unsigned char*)out, &new_len, NULL);
char mess[256];
if (r == LZO_E_OK && new_len == myInLen)
{
sprintf_s(mess, sizeof(mess), "解压成功!\n\n待解压文件大小: %lu bytes\n解压后文件大小: %lu bytes\n\n解压得到的文件“decompress.txt”保存于工程目录下",
(unsigned long)len, (unsigned long)new_len);
MessageBox(CString(mess));
}
else
{
/* this should NEVER happen */
sprintf_s(mess, sizeof(mess), "解压失败! %d\n", r);
MessageBox(CString(mess));
}
err = fopen_s(&ofp, ".\\decompress.txt", "wt+");
fwrite(out, sizeof(char), new_len, ofp);
fclose(ifp);
fclose(ofp);
free(cpTemp);
free(textPath);
free(out);
}
void CcompressDlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
#ifdef _UNICODE
LONG len1;
len1 = WideCharToMultiByte(CP_ACP, 0, path_decompress, -1, NULL, 0, NULL, NULL);
char *textPath = (char *)malloc(sizeof(char)*(len1 + 1));
memset(textPath, 0, len1 + 1);
WideCharToMultiByte(CP_ACP, 0, path_decompress, -1, textPath, len1 + 1, NULL, NULL);
#else
ptr = new char[str.GetAllocLength() + 1];
#endif
err = fopen_s(&ifp, textPath, "rb");
if (err == 0)
{
printf("Thefile'crt_fopen_s.c'was opened\n");
}
fseek(ifp, 0, SEEK_END);
unsigned long len = ftell(ifp);
fseek(ifp, 0, SEEK_SET);
cpTemp = (char*)malloc(len * sizeof(char));
fread(cpTemp, sizeof(char), len, ifp);
char *in = cpTemp;
char *out = (char *)malloc(len*100* sizeof(char));
memset(out, 0, len * 100 * sizeof(char));
int r;
lzo_uint new_len;
r = lzo1x_decompress((unsigned char*)in, len, (unsigned char*)out, &new_len, NULL);
char mess[256];
if (r == LZO_E_OK && new_len == myInLen)
{
sprintf_s(mess, sizeof(mess), "解压成功!\n\n待解压文件大小: %lu bytes\n解压后文件大小: %lu bytes\n\n解压得到的文件“decompress.txt”保存于工程目录下",
(unsigned long)len, (unsigned long)new_len);
MessageBox(CString(mess));
}
else
{
/* this should NEVER happen */
sprintf_s(mess, sizeof(mess), "解压失败! %d\n", r);
MessageBox(CString(mess));
}
err = fopen_s(&ofp, ".\\decompress.txt", "wt+");
fwrite(out, sizeof(char), new_len, ofp);
fclose(ifp);
fclose(ofp);
free(cpTemp);
free(textPath);
free(out);
}
界面效果如图: