   Zlib question
  I've passed from quite few time to c++ to play a bit on it.Trying to make a simple compression/decompression routine to/from gzip, but i'm encountering Z_DATA_ERROR on the same data i'm compressing.Here are my functions to compress/decompress data :int GZipManager::Compress(void* data, int datalength, void** dest){char* dst = new char[datalength+12 + (datalength / 100)];    z_stream stream;    int err;    stream.next_in = (Bytef*)data;    stream.avail_in = (uInt)datalength;    stream.next_out = (Bytef*)dst;    stream.avail_out = datalength+12 + (datalength / 100);    stream.zalloc = (alloc_func)0;    stream.zfree = (free_func)0;    stream.opaque = (voidpf)0;err = deflateInit2(&stream, Z_BEST_COMPRESSION,Z_DEFLATED, 16 + MAX_WBITS, 9, Z_DEFAULT_STRATEGY);    err = deflate(&stream, Z_FINISH);    if (err != Z_STREAM_END) {   deflateEnd(&stream);   return err == Z_OK ? Z_BUF_ERROR : err;    }    err = deflateEnd(&stream);*dest = dst;    return stream.total_out;}void GZipManager::Extract(void* data, int datalength, int uncompressedlength, void** dest){char* dst = new char[uncompressedlength];*dest = dst;z_stream stream;    int err;    stream.next_in = (Bytef*)data;    stream.avail_in = (uInt)datalength;    stream.next_out = (Bytef*)*dest;stream.avail_out = (uInt)uncompressedlength;    stream.zalloc = (alloc_func)0;    stream.zfree = (free_func)0;    err = inflateInit2(&stream, 32 + MAX_WBITS);    err = inflate(&stream, Z_FINISH);    if (err != Z_STREAM_END)    inflateEnd(&stream);    err = inflateEnd(&stream);}Am I doing something wrong? The Z_DATA_ERROR with msg "invalid block" appear on "err = inflate(&stream, Z_FINISH);"Any clues?   
  It depends on exactly where you're checking 'err', because you're calling inflateEnd() twice in that code. The code looks fine otherwise, but it has been a while since I last used zlib...   ;  I found my old zlib code. It's not pretty, but last time I checked, it worked fine.u8* compressData(u8* ddata,u32 dlength,u32* clength){  //compress the data 'ddata' of length 'dlength' into 'cdata' of length '*clength', returns 'cdata'  u8* cdata = 0;  if (ddata && (dlength > 0) && clength){    *clength = (u32)((dlength * 1.1) + 12);    cdata = (u8*)malloc(*clength);    if (cdata){ compress2(cdata,clength,ddata,dlength,Z_BEST_COMPRESSION);    }  }  return cdata;}u8* decompressData(u8* cdata,u32 clength,u32 dlength){  //decompress the data 'cdata' of length 'clength' into 'ddata' of length 'dlength', returns 'ddata'  u8* ddata = 0;    if (cdata && (clength > 0) && dlength){    ddata = (u8*)malloc(dlength);    if (ddata){ uncompress(ddata,&dlength,cdata,clength);    }  }  return ddata;}The clength and cdata variables refer to compressed data variables, and the dlength and ddata variables refer to decompressed data variables. From memory, I think the reason I used compress2() and uncompress() was because I had issues with the deflate and inflate functions. In any case, I should point out that there's no error handling done in this code. In any case, it should be fairly easy to adapt it to suit your code.   ;  There's also some gz* functions that are really simple to use.they work the same as fopen, fclose, fwrite, fread but they read and write gz (gzip) files.gzopen, gzclose, gzwrite, gzreadyou should check it out if you want to simplify   ;Quote:Original post by Atrix256There's also some gz* functions that are really simple to use.they work the same as fopen, fclose, fwrite, fread but they read and write gz (gzip) files.gzopen, gzclose, gzwrite, gzreadyou should check it out if you want to simplifyi need in-memory processing so its not a way. :(I'll do some more testing this week-end. :P   ;  Resolved, the Zlib code was okay...But i was opening the files in text mode (forgot a b in the mode) causing the result to be garbage...   ;  http://www.firstobject.com/easy-zlib-c++-xml-compression.htm is too easy.   