compressao_zlib.c
上传用户:syzginfo
上传日期:2021-02-26
资源大小:2k
文件大小:4k
- #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;
- }