If zlib compression fails because there's not enough room in the destination
buffer, deflateEnd will return Z_BUF_ERROR:

From the docs (http://www.zlib.net/manual.html):

	deflate() returns Z_OK if some progress has been made (more input
processed or more output produced), Z_STREAM_END if all input has been
consumed and all output has been produced (only when flush is set to
Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for
example if next_in or next_out was NULL), Z_BUF_ERROR if no progress is
possible (for example avail_in or avail_out was zero). Note that
Z_BUF_ERROR is not fatal, and deflate() can be called again with more
input and more output space to continue compressing.

Also, I don't think there's any reason to loop around deflate calls when
Z_FINISH is specified, I think deflate will return Z_OK the first time
it's called, then return Z_BUF_ERROR the second time it's called, because
no progress is possible.  From the docs:

	If the parameter flush is set to Z_FINISH, pending input is
processed, pending output is flushed and deflate returns with
Z_STREAM_END if there was enough output space; if deflate returns with
Z_OK, this function must be called again with Z_FINISH and more output
space (updated avail_out) but no more input data, until it returns with
Z_STREAM_END or an error.  After deflate has returned Z_STREAM_END, the
only possible operations on the stream are deflateReset or deflateEnd.

Reference #1005.