Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
compressao_zlib.c
Package: list_compress.zip [view]
Upload User: syzginfo
Upload Date: 2021-02-26
Package Size: 2k
Code Size: 4k
Category:
Compress-Decompress algrithms
Development Platform:
C/C++
- #include <stdio.h>
- #include <string.h>
- #include <assert.h>
- #include <zlib.h>
- #define CHUNK 16384
- /* Comprime de 'source' para 'dest' até o fim do buffer de arquivo
- inf() retorna Z_OK caso tudo ocorra bem, Z_MEM_ERROR se a memória nao pode
- ser alocada, Z_DATA_ERROR se o arquivo source contem erros(invalido ou
- imcompleto), Z_STREAM_ERROR se a taxa(level) de compressao estiver indisponivel,
- Z_VERSION_ERROR se a versao do cabeçalho zlib.h e a versao da biblioteca
- estatica nao batem, ou Z_ERRNO se há um erro ao ler ou escrever
- os arquivos */
- int comprime(FILE *source, FILE *dest, int level)
- {
- int ret, flush;
- unsigned have;
- z_stream strm;
- unsigned char in[CHUNK];
- unsigned char out[CHUNK];
- strm.zalloc = Z_NULL;
- strm.zfree = Z_NULL;
- strm.opaque = Z_NULL;
- ret = deflateInit(&strm, level);
- if (ret != Z_OK)
- return ret;
- /* comprime ate o fim do arquivo */
- 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;
- /* executa deflate() na entrada enquanto o buffer de saida nao estiver
- cheio, termina compressao se 'source' tiver sido lido completamente */
- do {
- strm.avail_out = CHUNK;
- strm.next_out = out;
- ret = deflate(&strm, flush);
- assert(ret != Z_STREAM_ERROR);
- have = CHUNK - strm.avail_out;
- if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
- (void)deflateEnd(&strm);
- return Z_ERRNO;
- }
- } while (strm.avail_out == 0);
- assert(strm.avail_in == 0); /* toda a entrada sera usada*/
- /* done quando o ultimo dado do arquivo for processado */
- } while (flush != Z_FINISH);
- assert(ret == Z_STREAM_END); /* a stream estara completa*/
- /* limpa buffer e retorna */
- (void)deflateEnd(&strm);
- return Z_OK;
- }
- /* Descomprime de 'source' para 'dest' até o fim do buffer de arquivo
- inf() retorna Z_OK caso tudo ocorra bem, Z_MEM_ERROR se a memória nao pode
- ser alocada, Z_DATA_ERROR se o arquivo source contem erros(invalido ou
- imcompleto), Z_VERSION_ERROR se a versao do cabeçalho zlib.h e a versao
- da biblioteca estatica nao batem, ou Z_ERRNO se há um erro ao ler ou escrever
- os arquivos */
- int descomprime(FILE *source, FILE *dest)
- {
- int ret;
- unsigned have;
- z_stream strm;
- unsigned char in[CHUNK];
- unsigned char out[CHUNK];
- 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;
- 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;
- do {
- strm.avail_out = CHUNK;
- strm.next_out = out;
- ret = inflate(&strm, Z_NO_FLUSH);
- assert(ret != Z_STREAM_ERROR);
- switch (ret) {
- case Z_NEED_DICT:
- ret = Z_DATA_ERROR;
- case Z_DATA_ERROR:
- case Z_MEM_ERROR:
- (void)inflateEnd(&strm);
- return ret;
- }
- have = CHUNK - strm.avail_out;
- if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
- (void)inflateEnd(&strm);
- return Z_ERRNO;
- }
- } while (strm.avail_out == 0);
- /* done quando inflate() diz q chegou o fim do arquivo */
- } while (ret != Z_STREAM_END);
- /* limpa buffer e retorna*/
- (void)inflateEnd(&strm);
- return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
- }