C + + class for implementing Lzari compression algorithm

Source: Internet
Author: User
Tags uncompress

This is a class of data compression based on the Lzari algorithm. Haruhiko Okumura the algorithm in C language on July 4, 1989. But it uses some global or static variables, which are inconvenient to use under MFC. I changed it into a C + + class, Make it easy to compress and decompress, and more importantly, I've added two new interfaces that can compress/decompress a memory buffer, not just a file.

A total of 5 external interfaces were provided:

1. Compress/Decompress files

void Compress(const char *lpszInfile,const char *lpszOutfile);
void UnCompress(const char *lpszInfile,const char *lpszOutfile);

Parameters at a glance, you can use these two interfaces as follows:LZARI Lzari;
Lzari.Compress("show.bmp","show.liz");  //压缩文件 show.bmp 到 show.liz
// Lzari.UnCompress("show.liz","show.bmp"); // 解压缩文件 show.liz 到 show.bmp

It's as simple as that.

2. Compress/Decompress a section of memory buffer

void Compress(const BYTE *pInBuffer,int nInLength,const BYTE * &pOutBuffer ,int &nOutLength);
void UnCompress(const BYTE *pInBuffer,int nInLength,const BYTE * &pOutBuffer,int &nOutLength);

The parameters of these two interfaces are also easy to understand, passing in input pointers and lengths, and Lzari will return a read-only output pointer and length. The user does not have to worry about memory allocation, and when it is not necessary to use the output result, call Release (), and here's how to use the example:

LZARI Lzari;
BYTE *pOutBuffer = NULL;
int nOutSize = 0;
char szInBuffer[] = "This is a class for compress and uncompress";
Lzari.Compress(szInBuffer,strlen(szInBuffer),pOutBuffer,nOutSize);//压缩pInBuffer
//
// 用pOutBuffer 做一些事情
//
Lzari.Release();

3. Free the memory and clear the flag.

void Release();

If you want a Lzari class instance to be both compressed and uncompressed, call release () before the next action call, as follows: Lzari Lzari
Lzari.compress (pinbuffer,ninsize,poutbuffer,noutsize)//Compress Pinbuffer
//
//Do something with Poutbuffer
//
Lzari.release ();
Lzari.uncompress (PINBUFFER2,NINSIZE2,POUTBUFFER2,NOUTSIZE2);//Decompress PInBuffer2
//
//...
//
Lzari.release ();
Please be careful not to call this: lzari.compress (pinbuffer,ninsize,poutbuffer,noutsize);//Compress Pinbuffer
//
//with Poutbuffer Do something
//
Lzari.release ();
Lzari.uncompress (POUTBUFFER,NOUTSIZE,POUTBUFFER2,NOUTSIZE2); Uncompress results for first compression

The Poutbuffer pointer after release () is invalid. If you do not call release (), it can cause poutbuffer and pOutBuffer2 to point to the same memory and cause confusion. It is best to use two class instances to accomplish this.LZARI Lzari;
LZARI UnLzari;
Lzari.Compress(pInBuffer,nInsize,pOutBuffer,nOutSize);//压缩pInBuffer
//
// ...
//
UnLzari.UnCompress(pOutBuffer,nOutSize,pOutBuffer2,nOutSize2); //解压缩第一次压缩的结果
//
// ...
//
Lzari.Release();
UnLzari.Release();

Because the program uses the STL vector template, add the following line to the stdafx.h: #include <vector> Of course, this class is not dependent on MFC and can be used in any C + + program.

In addition, Lzari compression effect than zip Some, the gap is about 5%~10%, compression speed is basically equivalent.

Note: Questions related to algorithms please don't ask me, I don't know:) Other questions Welcome Advice querw@sina.com

This article supporting source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.