Search code examples
c++c++11zlib

zlib gzclose: How to detect a successful file closure?


I have used zlib to compress a file. All works well. After the operation is complete, I call gzclose(file) to flush and close the gzip file. According to the documentation, the gzclose returns an int which provides the success or failure of the gzclose operation. Since there can be many failure reasons, checking for each error code is not feasible for me. I opt to check for the success of the operation and handle error codes separately. I could not find the return code for successful gzclose in any of the documentation I referred.

doc


Solution

  • The zlib functions are documented in zlib.h. You can also find zlib.h formatted a bit in the zlib Manual. In there you find:

    ZEXTERN int ZEXPORT gzclose OF((gzFile file));

    Flushes all pending output if necessary, closes the compressed file and deallocates the (de)compression state. Note that once file is closed, you cannot call gzerror with file, since its structures have been deallocated. gzclose must not be called more than once on the same file, just as free must not be called more than once on the same allocation. gzclose will return Z_STREAM_ERROR if file is not valid, Z_ERRNO on a file operation error, Z_MEM_ERROR if out of memory, Z_BUF_ERROR if the last read ended in the middle of a gzip stream, or Z_OK on success.