compress.c
上传用户:zlh9724
上传日期:2007-01-04
资源大小:1991k
文件大小:2k
源码类别:

浏览器

开发平台:

Unix_Linux

  1. /* compress.c -- compress a memory buffer
  2.  * Copyright (C) 1995 Jean-loup Gailly.
  3.  * For conditions of distribution and use, see copyright notice in zlib.h 
  4.  */
  5. /* $Id: compress.c,v 1.6 1995/05/03 17:27:08 jloup Exp $ */
  6. #include "zlib.h"
  7. /* ===========================================================================
  8.      Compresses the source buffer into the destination buffer.  sourceLen is
  9.    the byte length of the source buffer. Upon entry, destLen is the total
  10.    size of the destination buffer, which must be at least 0.1% larger than
  11.    sourceLen plus 8 bytes. Upon exit, destLen is the actual size of the
  12.    compressed buffer.
  13.      This function can be used to compress a whole file at once if the
  14.    input file is mmap'ed.
  15.      compress returns Z_OK if success, Z_MEM_ERROR if there was not
  16.    enough memory, Z_BUF_ERROR if there was not enough room in the output
  17.    buffer.
  18. */
  19. int compress (dest, destLen, source, sourceLen)
  20.     Bytef *dest;
  21.     uLongf *destLen;
  22.     Bytef *source;
  23.     uLong sourceLen;
  24. {
  25.     z_stream stream;
  26.     int err;
  27.     stream.next_in = source;
  28.     stream.avail_in = (uInt)sourceLen;
  29.     /* Check for source > 64K on 16-bit machine: */
  30.     if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
  31.     stream.next_out = dest;
  32.     stream.avail_out = (uInt)*destLen;
  33.     if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
  34.     stream.zalloc = (alloc_func)0;
  35.     stream.zfree = (free_func)0;
  36.     err = deflateInit(&stream, Z_DEFAULT_COMPRESSION);
  37.     if (err != Z_OK) return err;
  38.     err = deflate(&stream, Z_FINISH);
  39.     if (err != Z_STREAM_END) {
  40.         deflateEnd(&stream);
  41.         return err == Z_OK ? Z_BUF_ERROR : err;
  42.     }
  43.     *destLen = stream.total_out;
  44.     err = deflateEnd(&stream);
  45.     return err;
  46. }