Name

compress — Attempts to compress sourceLen bytes of data in the buffer source, placing the result in the buffer dest.


Library

Libz.lib


Synopsis

#include <zlib.h>
int compress(Bytef * dest, uLongf * destLen, const Bytef * source, uLong sourceLen);  

Return Value

On success, compress() shall return Z_OK, otherwise compress() returns a value to indicate the error.       

Detailed Description

The compress() function attempts to compress sourceLen bytes of data in the buffer source, placing the result in the buffer dest.

On entry, destLen points to a value describing the size of the dest buffer. The application should ensure that this value should atleast be (sourceLen × 1.001) + 12. On successful exit, the variable referenced by destLen shall be updated to hold the length of compressed data in dest.

The compress() function is equivalent to compress2() with a level of Z_DEFAULT_LEVEL.


Examples

To compress string "hello" with level value 6:

#include <stdio.h> 
#include <zlib.h>
void Compress2()
{
    uLong len = (uLong)strlen(hello)+1;
    const char hello[] = "hello, hello!";
    Byte *compr, *uncompr;
    uLong comprLen = 20*sizeof(int);
    compr = (Byte*)calloc((uInt)comprLen, 1);
   compress2(compr, &comprLen, (const Bytef*)hello, len,6);    
    free(compr);
}

Output

compr contains the compressed data.                                                  

Errors

On error, compress() shall return a value as described below:

Z_BUF_ERROR

The buffer dest was not large enough to hold the compressed data.

Z_MEM_ERROR

Insufficient memory.


Feedback

For additional information or queries on this page send feedback


© 2005-2007 Nokia

Top