$wget http://www.zlib.net/zlib-1.2.3.tar.gz
$tar-xvzf zlib-1.2.3.tar.gz
$cd zlib-1.2.3.tar.gz
$. /configure
$make
$sudo make install
Once installed, it's ready to use.
Zlibmgr.h file
#ifndef _zlibmgr #define _ZLIBMGR #define MaxBufferSize 200000 #include <iostream> #include <string.h> #inclu
De <stdlib.h> #include <zlib.h> using namespace std;
Class Czlibmgr {public:czlibmgr ();
~czlibmgr (); BOOL Compress (const char* pccontentbuf, char* pccompbuf, unsigned long&); Compression, pccontentbuf to compress content pccompbuf compressed content Ulcomplen after compression length bool Uncompress (const char* pccompbuf, char* pcuncompbuf, unsigned long Ulcomplen);
Decompression, Pccompbuf compressed content, pcuncompbuf the content Ulcomplen compressed content after the length private:byte compr[maxbuffersize];
Byte Uncompr[maxbuffersize];
}; Czlibmgr::czlibmgr () {} czlibmgr::~czlibmgr () {} bool Czlibmgr::compress (const char* pccontentbuf, char* pcCompBuf, UN
Signed long& Ulcomplen) {if (pccontentbuf = = NULL) {return false;
} if (strlen (pccontentbuf) = = 0) {return false;
} memset (Compr, 0, MaxBufferSize);
ULong Comprlen;
int err; UlonG len = strlen (PCCONTENTBUF);
Comprlen = sizeof (COMPR)/sizeof (compr[0));
Err = Compress (Compr, &comprlen, (const bytef*) pccontentbuf, Len);
if (Err!= z_ok) {cout << "compess error:" << err << Endl;
return false;
} cout << "orignal size:" << len << ", Compressed Size:" << comprlen << Endl;
memcpy (Pccompbuf, COMPR, Comprlen);
Ulcomplen = Comprlen;
return true; BOOL Czlibmgr::uncompress (const char* pccompbuf, char* pcuncompbuf, unsigned long Ulcomplen) {if (Pccompbuf = = NUL L) {cout <<__function__ << "================> pccompbuf is null in-check" << end
L
return false; } if (strlen (pccompbuf) = = 0) {cout <<__function__ << "strlen (pccompbuf) = = 0 =============
===========> "<< Endl;
return false;
} memset (UNCOMPR, 0, MaxBufferSize); ULong Uncomprlen = MaxbuFfersize;
int err;
Err = uncompress (UNCOMPR, &uncomprlen, (const BYTEF *) PCCOMPBUF, Ulcomplen);
if (Err!= z_ok) {cout << "uncompess error:" << err << Endl;
return false; } cout << "Compress size:" << ulcomplen << "uncompressed size:" << uncomprlen << E
Ndl
memcpy (Pcuncompbuf, UNCOMPR, Uncomprlen);
return true;
} czlibmgr G_kzlibmgr; #endif
Examples to use
#include <string.h> #include <stdlib.h> #include <iostream> #include <zlib.h> #include
Zlibmgr.h "using namespace std;
extern czlibmgr G_kzlibmgr;
int main () {cout << sizeof (long) << "" << sizeof (int) << Endl;
Long uu1 = 10000;
unsigned int i1 = 0;
I1 = (unsigned int) uu1;
cout << i1 << Endl;
int err; Byte compr[200000], uncompr[200000];
Big enough memset (COMPR, 0, 200000);
memset (UNCOMPR, 0, 200000);
ULong Comprlen, Uncomprlen;
Const char* Hello = "guosyidlsldkksldkieeeeeeeee211111111111111111111111111111111111\" ";
unsigned long U1, U2;
u1 = 0;
U2 = 0;
Char soutbuf[8096];
G_kzlibmgr.compress (Hello, Soutbuf, U1);
cout << "======================>" << U1 << Endl;
Char suncompressbuf[8096];
memset (suncompressbuf, 0, sizeof (SUNCOMPRESSBUF));
G_kzlibmgr.uncompress (Soutbuf, Suncompressbuf, U1); cout << SuncompRessbuf << Endl; }
Compile in the following manner
g++-O testzlib testzlib.cpp-lz
You can take the zlibmgr directly to use, compression effect is very good usually in more than half