tests/zdeflate.c
/* 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. */
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.
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 strategy, int isZlib, char* dictionary, int dictSize)
{
    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 = deflateInit2(&strm, level, Z_DEFLATED, isZlib?15:-15, 8, strategy);
    if (ret != Z_OK)
        return ret;
    if (dictionary)
        deflateSetDictionary(&strm, dictionary, dictSize);
    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;
        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);     
    } while (flush != Z_FINISH);
    assert(ret == Z_STREAM_END);        
    (void)deflateEnd(&strm);
    return Z_OK;
}
int def ( FILE * source , FILE * dest , int level , int strategy , int isZlib , char * dictionary , int dictSize ) { 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 = deflateInit2 ( & strm , level , Z_DEFLATED , isZlib ? 15 : -15 , 8 , strategy ) ; if ( ret != Z_OK ) return ret ; if ( dictionary ) deflateSetDictionary ( & strm , dictionary , dictSize ) ; 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 ; 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 ) ; } while ( flush != Z_FINISH ) ; assert ( ret == Z_STREAM_END ) ; ( void ) deflateEnd ( & strm ) ; return Z_OK ; }
Compress from file source to file dest until EOF on source.
Compress from file source to file dest until EOF on source .
/* allocate deflate state */ /* compress until end of file */ /* run deflate() on input until output buffer not full, finish
           compression if all of source has been read in */ /* no bad return value */ /* state not clobbered */ /* all input will be used */ /* done when last data in file processed */ /* stream will be complete */ /* clean up and return */