New Post has been published on ITESSAYS
New Post has been published on http://itessays.com/linux-technology/installation-and-use-zlib-library.html
Installation and use Zlib library
In practical applications often encounter problems to compress the data, common compression format zip and rar, but under Linux it is more of, bz2, gz, xz and so on, decompression and compression commands only under Linux there is a lot of it. Check the information, zlib it should be relatively easy to use, and it is widely used, so ready to use this one.
Download Zlib library address: http://zlib.net/zlib128.zip .Use wget to download, unzip it and then use it, then as general software like ./configure && make && make install (Note to root privileges).
cp libz.a /usr/local/lib chmod 644 /usr/local/lib/libz.a cp libz.so.1.2.8 /usr/local/lib chmod 755 /usr/local/lib/libz.so.1.2.8 cp zlib.3 /usr/local/share/man/man3 chmod 644 /usr/local/share/man/man3/zlib.3 cp zlib.pc /usr/local/lib/pkgconfig chmod 644 /usr/local/lib/pkgconfig/zlib.pc cp zlib.h zconf.h /usr/local/include chmod 644 /usr/local/include/zlib.h /usr/local/include/zconf.h
Write a simple example to test, pay attention when compiling this library to join -lz
#include <stdio.h> #include <zlib.h> int main(int argc,char **args) /* source data */ unsigned char strsrc[]=" this is source data123456789 abcdefghigklmnopqrstuvwxyznt abcdefghijklmnopqrstuvwxyzn"; // include Character unsigned char buf[1024]=0; unsigned char strdst[1024]=0; unsigned long srclen=sizeof(strsrc); unsigned long buflen=sizeof(buf); unsigned long dstlen=sizeof(strdst); int i; FILE * fp; printf("souece str"); for(i=0;i<srclen;++i) printf("%c",strsrc[i]); printf("souece str:%ldn",srclen); printf("length:%ldn",compressBound(srclen)); //Compression compress(buf,&buflen,strsrc,srclen); printf("The actual length after compression:%ldn",buflen); //Unzip uncompress(strdst,&dstlen,buf,buflen); printf("target str:"); for(i=0;i<dstlen;++i) printf("%c",strdst[i]); return 0;
API
// Compressed into the source buffer to a destination buffer
int compress (Bytef * dest, uLongf * destLen, const Bytef * source, uLong sourceLen);
// Function as the same with compress function, one more parameter to specify the compression quality and compression relationship (0-9). To obtain a high compression ratio to spend time
int compress2 (Bytef * dest, uLongf * destLen, const Bytef * source, uLong sourceLen, int level);
// Calculate the required buffer length. Suppose you wanted to know before compression length sourcelen after you how much data compression, you can call this function calculation, this function does not get accurate results, but it can ensure that the actual output length it is certainly less than the length calculated
uLong compressBound (uLong sourceLen);
// Decompression
int uncompress (Bytef * dest, uLongf * destLen, const Bytef * source, uLong sourceLen);
Handle gz suffix compressed files.
Compressed string to test.gz
#include <stdio.h> #include <zlib.h> int main(int argc,char **args) gzFile file; char str[]="testtest"; file=gzopen("test.gz","wb"); if(NULL==file) perror("Can't open file"); gzsetparams(file,2,0); gzwrite(file,str,sizeof(str)); gzclose(file); return 0;
Unzip test.gz
#include <stdio.h> #include <zlib.h> int main(int argc,char **args) gzFile file; char str[64]=0; file=gzopen("test.gz","rb"); if(NULL==file) perror("Can't open file"); gzread(file,str,10); printf("To read characters from a file:%s.n",str); gzclose(file); return 0;
Generally, all multiple files using tar first pressed into a bag, then in gzip. This is why the downloaded software source package mostly .tar.gz format.
API to handle gz files.
//The following describes the functions, with the general file handler is similar, the effect is the same. typedef voidp gzFile; //Open a gzip file to read/write, mode and fopen ("rb" or "wb"), as the compression level can also be included, such as:"Wb9", or with a policy "f" as the filter data "wb6f", "h" is to "huffman" compression, such as:"wb1h". Giopen use for reading a file without no gzip format .gzread read data directly from no unzipped file. If the file can not be opened or not enough memory, gzopen will return NULL. gzFile gzopen(const char *path,const char *mode); //Open a gz file based on the file description gzFile gzdopen (int fd, const char *mode); //Dynamically update the compression level and compression format, successful return Z_OK, otherwise Z_STREAM_ERROR int gzsetparams (gzFile file, int level, int strategy); //Reads the given number of uncompressed bytes from the compressed file.If the input file was not in gzip format, gzread copies the given number of bytes into the buffer. int gzread (gzFile file,voidp buf, unsigned int len); //Given the length of the buf write characters to the file, successfully returns the number of characters, 0 indicates write failure int gzwrite(gzFile file, voidp buf,unsigned int len); //Here you can see all the functions related to the handling of our ordinary files, replaced the gz to f, is not to be familiar with? int gzprintf(gzFile file,const char *format, ...); int gzputs(gzFile file,const char *s); char * gzgets(gzFile file, char *buf,int len); int gzputc(gzFile file,int c); int gzgetc(gzFile file); int gzungetc(int c,gzFile file); int gzflush (gzFile file,int flush); int gzseek (gzFile file,z_off_t offset, int whence); int gzrewind(gzFile file); z_off_t gztell(gzFile file); int gzeof(gzFile file); int gzdirect(gzFile file); int gzclose(gzFile file); const char * gzerror(gzFile file, int *errnum); void gzclearerr(gzFile file);
Calculate checksum
uLong adler32 (uLong adler,const Bytef *buf, uInt len); //use method: uLong adler=adler32(0L,Z_NULL,0); while(read_buffer(buffer, length) !=EOF) adler=adler32(adler,buffer,length); if(adler != original_adler) error(); uLong crc32 (uLong crc,const Bytef *buf,uInt len); //use method: uLong crc = crc32(0L,Z_NULL,0); while(read_buffer(buffer,length)!=EOF) crc=crc32(crc,buffer,length); if(crc != original_crc) error();
Finally, the structure of z_stream
typedef struct z_stream_s Bytef *next_in; /* next input byte */ uInt avail_in; /* number of bytes available at next_in */ uLong total_in; /* total nb of input bytes read so far */ Bytef *next_out; /* next output byte should be put there */ uInt avail_out; /* remaining free space at next_out */ uLong total_out; /* total nb of bytes output so far */ char *msg; /* last error message, NULL if no error */ struct internal_state FAR *state; /* not visible by applications */ alloc_func zalloc; /* used to allocate the internal state */ free_func zfree; /* used to free the internal state */ voidpf opaque; /* private data object passed to zalloc and zfree */ int data_type; /* best guess about the data type: binary or text */ uLong adler; /* adler32 value of the uncompressed data */ uLong reserved; /* reserved for future use */ z_stream;
deflateInit() + deflate() + deflateEnd() //3 function in combination with the completion of the compression function, specific usage see example.c of test_deflate() function. Actually compress() function is used inside these three functions to achieve inflateInit() + inflate() + inflateEnd() //Complete decompression functions.
Here’s an example, to understand the usage function, but in general the application can use the above function.
/* zpipe.c: example of proper use of zlib's inflate() and deflate() Not copyrighted -- provided to the public domain Version 1.4 11 December 2005 Mark Adler */ /* Version history: 1.0 30 Oct 2004 First version 1.1 8 Nov 2004 Add void casting for unused return values Use switch statement for inflate() return values 1.2 9 Nov 2004 Add assertions to document zlib guarantees 1.3 6 Apr 2005 Remove incorrect assertion in inf() 1.4 11 Dec 2005 Add hack to avoid MSDOS end-of-line conversions Avoid some compiler warnings for input and output buffers */ #include <stdio.h> #include <string.h> #include <assert.h> #include "zlib.h" #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__) # include <fcntl.h> # include <io.h> # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) #else # define SET_BINARY_MODE(file) #endif #define CHUNK 16384 /* Compress from file source to file dest until EOF on source. def() returns Z_OK on success, Z_MEM_ERROR if memory could not be allocated for processing, Z_STREAM_ERROR if an invalid compression level is supplied, Z_VERSION_ERROR if the version of zlib.h and the version of the library linked do not match, or Z_ERRNO if there is an error reading or writing the files. */ int def(FILE *source, FILE *dest, int level) int ret, flush; unsigned have; z_stream strm; unsigned char in[CHUNK]; unsigned char out[CHUNK]; /* allocate deflate state */ strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; ret = deflateInit(&strm, level); if (ret != Z_OK) return ret; /* compress until end of file */ do strm.avail_in = fread(in, 1, CHUNK, source); if (ferror(source)) (void)deflateEnd(&strm); return Z_ERRNO; flush = feof(source) ? Z_FINISH : Z_NO_FLUSH; strm.next_in = in; /* run deflate() on input until output buffer not full, finish compression if all of source has been read in */ do ferror(dest)) (void)deflateEnd(&strm); return Z_ERRNO; while (strm.avail_out == 0); assert(strm.avail_in == 0); /* all input will be used */ /* done when last data in file processed */ while (flush != Z_FINISH); assert(ret == Z_STREAM_END); /* stream will be complete */ /* clean up and return */ (void)deflateEnd(&strm); return Z_OK; /* Decompress from file source to file dest until stream ends or EOF. inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be allocated for processing, Z_DATA_ERROR if the deflate data is invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and the version of the library linked do not match, or Z_ERRNO if there is an error reading or writing the files. */ int inf(FILE *source, FILE *dest) int ret; unsigned have; z_stream strm; unsigned char in[CHUNK]; unsigned char out[CHUNK]; /* allocate inflate state */ strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; strm.avail_in = 0; strm.next_in = Z_NULL; ret = inflateInit(&strm); if (ret != Z_OK) return ret; /* decompress until deflate stream ends or end of file */ do strm.avail_in = fread(in, 1, CHUNK, source); if (ferror(source)) (void)inflateEnd(&strm); return Z_ERRNO; if (strm.avail_in == 0) break; strm.next_in = in; /* run inflate() on input until output buffer not full */ do while (strm.avail_out == 0); /* done when inflate() says it's done */ while (ret != Z_STREAM_END); /* clean up and return */ (void)inflateEnd(&strm); return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR; /* report a zlib or i/o error */ void zerr(int ret) fputs("zpipe: ", stderr); switch (ret) case Z_ERRNO: if (ferror(stdin)) fputs("error reading stdinn", stderr); if (ferror(stdout)) fputs("error writing stdoutn", stderr); break; case Z_STREAM_ERROR: fputs("invalid compression leveln", stderr); break; case Z_DATA_ERROR: fputs("invalid or incomplete deflate datan", stderr); break; case Z_MEM_ERROR: fputs("out of memoryn", stderr); break; case Z_VERSION_ERROR: fputs("zlib version mismatch!n", stderr); /* compress or decompress from stdin to stdout */ int main(int argc, char **argv) int ret; /* avoid end-of-line conversions */ SET_BINARY_MODE(stdin); SET_BINARY_MODE(stdout); /* do compression if no arguments */ if (argc == 1) ret = def(stdin, stdout, Z_DEFAULT_COMPRESSION); if (ret != Z_OK) zerr(ret); return ret; /* do decompression if -d specified */ else if (argc == 2 && strcmp(argv[1], "-d") == 0) ret = inf(stdin, stdout); if (ret != Z_OK) zerr(ret); return ret; /* otherwise, report usage */ else fputs("zpipe usage: zpipe [-d] < source > destn", stderr); return 1;
Web server for sending gzip compression
#include <string.h> #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <signal.h> #include <zlib.h> #include <zconf.h> void app_exit(); int socket_listen(u_short port); void send_http_head(int clifd); void put_long (unsigned char *string, unsigned long x); int gzip_http_buffer(int clifd,char *msg,int len); int sockfd; int main(int argc,char **args) struct sockaddr_in cli_sin; socklen_t cli_len=sizeof(cli_sin); int clifd; char buf[4096]; char msg[4096]="<br><br><h1>Reage Web Server gzip support text!</h1><br/><h1><a href = "http://wunaozai.cnblogs.com">Reage blog</a>"; signal(SIGINT,app_exit); sockfd=socket_listen(8080); while(1) clifd=accept(sockfd,(struct sockaddr *)&cli_sin,&cli_len); printf("From IP:%s:%un",inet_ntoa(cli_sin.sin_addr),ntohs(cli_sin.sin_port)); read(clifd,buf,4096); printf("%sn",buf); send_http_head(clifd); gzip_http_buffer(clifd,msg,strlen(msg)); close(clifd); close(sockfd); return 0; void put_long (unsigned char *string, unsigned long x) string[0] = (x & 0xff); string[1] = ((x >> 8) & 0xff) ; string[2] = ((x >> 16) & 0xff) ; string[3] = ((x >> 24) & 0xff); /* * Sending compressed characters * * */ static const char gzip_header[10]=0x1f,0x8b,0x08,0,0,0,0,0,0,0x03; int gzip_http_buffer(int clifd,char *msg,int len) z_stream stream; int ret,flush; char in[4096];//Data stored in the input char send[4096+18];//Stored after the data is compressed unsigned int have; int tmp; memcpy(send,gzip_header,10); memset(in,0,len); stream.zalloc=Z_NULL; stream.zfree=Z_NULL; stream.opaque=Z_NULL; stream.avail_in=0; stream.next_in=Z_NULL; memcpy(in,msg,len); //Compression initialization tmp=deflateInit2(&stream, Z_DEFAULT_COMPRESSION,//Compression level, from 0-9 Z_DEFLATED,//Compression Type -MAX_WBITS, 8, Z_DEFAULT_STRATEGY); if(Z_OK!=tmp) perror("deflateInit2 Error"); return 0; stream.avail_in = len; //Length of the data to be compressed stream.next_in = in; //First address to the compressed data stream.avail_out = 4096; //The maximum length can be stored in the output. Is the maximum length of the compressed data stream.next_out = send + 10; //Store compressed data starting position, send the first ten bytes used to put the head ret = deflate (&stream,Z_FINISH); //Compression switch(ret) case Z_NEED_DICT: ret=Z_DATA_ERROR; case Z_DATA_ERROR: case Z_MEM_ERROR: (void)inflateEnd(&stream); return ret; have = 4096 - stream.avail_out; unsigned crc = crc32(0L, in, len); char * tail = send + 10 + have; put_long (tail, crc); put_long (tail + 4, len); write (clifd, send, have + 18); deflateEnd (&stream); return 1; void app_exit() signal(SIGINT,SIG_DFL); close(sockfd); exit(0); /* * Send a HTTP Header * */ void send_http_head(int clifd) char buf[4096]; memset(buf,0,sizeof(buf)); sprintf(buf,"HTTP/1.1 200 OKrn"); sprintf(buf,"%sServer:wunaozai.cnblogs.comrn",buf); sprintf(buf,"%sContent-Encoding: gziprn",buf);//Tell the browser, the data is compressed by gzip sprintf(buf,"%sContent-Type: text/htmlrnrn",buf); write(clifd,buf,strlen(buf)); /* * */ int socket_listen(u_short port) struct sockaddr_in sin; int on; int httpd=socket(PF_INET,SOCK_STREAM,0); if(httpd==-1) perror("Fail to Socket"); //init sockaddr_in sin.sin_family=AF_INET; sin.sin_port=htons(port); sin.sin_addr.s_addr=htonl(INADDR_ANY); bzero(&(sin.sin_zero),8); setsockopt(httpd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)); if(bind(httpd,(struct sockaddr *)&sin,sizeof(struct sockaddr))==-1) perror("Fail to bind"); if(port==0) socklen_t len=sizeof(sin); if(getsockname(httpd,(struct sockaddr *)&sin,&len)==-1) perror("Fail to getsockname"); port=ntohs(sin.sin_port); if(listen(httpd,100)<0) perror("Fail to listen"); printf("open port:%un",port); return httpd;
.CPlase_panel display:none;















