diff --git a/C-Sources/ModelicaMatIO.c b/C-Sources/ModelicaMatIO.c
index acfe5f4..239b639 100644
--- a/C-Sources/ModelicaMatIO.c
+++ b/C-Sources/ModelicaMatIO.c
@@ -12035,35 +12035,39 @@ WriteCompressedCharData(mat_t *mat,z_stream *z,void *data,int N,
         case MAT_T_UINT16:
         {
             data_size = 2;
-            data_tag[0]  = MAT_T_UINT16;
-            data_tag[1]  = N*data_size;
-            z->next_in   = ZLIB_BYTE_PTR(data_tag);
-            z->avail_in  = 8;
-            z->next_out  = buf;
-            z->avail_out = buf_size;
-            err = deflate(z,Z_PARTIAL_FLUSH);
-            byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
+            data_tag[0] = MAT_T_UINT16;
+            data_tag[1] = N*data_size;
+            z->next_in  = ZLIB_BYTE_PTR(data_tag);
+            z->avail_in = 8;
+            do {
+                z->next_out  = buf;
+                z->avail_out = buf_size;
+                err = deflate(z,Z_NO_FLUSH);
+                byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
+            } while ( z->avail_out == 0 );
 
             /* exit early if this is a empty data */
             if ( NULL == data || N < 1 )
                 break;
 
-            z->next_in   = data;
-            z->avail_in  = data_size*N;
+            z->next_in  = data;
+            z->avail_in = data_size*N;
             do {
                 z->next_out  = buf;
                 z->avail_out = buf_size;
-                err = deflate(z,Z_PARTIAL_FLUSH);
+                err = deflate(z,Z_NO_FLUSH);
                 byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
             } while ( z->avail_out == 0 );
             /* Add/Compress padding to pad to 8-byte boundary */
             if ( N*data_size % 8 ) {
-                z->next_in   = pad;
-                z->avail_in  = 8 - (N*data_size % 8);
-                z->next_out  = buf;
-                z->avail_out = buf_size;
-                err = deflate(z,Z_PARTIAL_FLUSH);
-                byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
+                z->next_in  = pad;
+                z->avail_in = 8 - (N*data_size % 8);
+                do {
+                    z->next_out  = buf;
+                    z->avail_out = buf_size;
+                    err = deflate(z,Z_NO_FLUSH);
+                    byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
+                } while ( z->avail_out == 0 );
             }
             break;
         }
@@ -12074,76 +12078,86 @@ WriteCompressedCharData(mat_t *mat,z_stream *z,void *data,int N,
             mat_uint16_t c;
 
             /* Matlab can't read MAT_C_CHAR as uint8, needs uint16 */
-            data_size    = 2;
-            data_tag[0]  = MAT_T_UINT16;
-            data_tag[1]  = N*data_size;
-            z->next_in   = ZLIB_BYTE_PTR(data_tag);
-            z->avail_in  = 8;
-            z->next_out  = buf;
-            z->avail_out = buf_size;
-            err = deflate(z,Z_PARTIAL_FLUSH);
-            byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
+            data_size   = 2;
+            data_tag[0] = MAT_T_UINT16;
+            data_tag[1] = N*data_size;
+            z->next_in  = ZLIB_BYTE_PTR(data_tag);
+            z->avail_in = 8;
+            do {
+                z->next_out  = buf;
+                z->avail_out = buf_size;
+                err = deflate(z,Z_NO_FLUSH);
+                byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
+            } while ( z->avail_out == 0 );
 
             /* exit early if this is a empty data */
             if ( NULL == data || N < 1 )
                 break;
 
-            z->next_in   = data;
-            z->avail_in  = data_size*N;
+            z->next_in  = data;
+            z->avail_in = data_size*N;
             ptr = data;
             for ( i = 0; i < N; i++ ) {
                 c = (mat_uint16_t)*(char *)ptr;
-                z->next_in   = ZLIB_BYTE_PTR(&c);
-                z->avail_in  = 2;
-                z->next_out  = buf;
-                z->avail_out = buf_size;
-                err = deflate(z,Z_PARTIAL_FLUSH);
-                byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
+                z->next_in  = ZLIB_BYTE_PTR(&c);
+                z->avail_in = 2;
+                do {
+                    z->next_out  = buf;
+                    z->avail_out = buf_size;
+                    err = deflate(z,Z_NO_FLUSH);
+                    byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
+                } while ( z->avail_out == 0 );
                 ptr++;
             }
             /* Add/Compress padding to pad to 8-byte boundary */
             if ( N*data_size % 8 ) {
-                z->next_in   = pad;
-                z->avail_in  = 8 - (N*data_size % 8);
-                z->next_out  = buf;
-                z->avail_out = buf_size;
-                err = deflate(z,Z_PARTIAL_FLUSH);
-                byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
+                z->next_in  = pad;
+                z->avail_in = 8 - (N*data_size % 8);
+                do {
+                    z->next_out  = buf;
+                    z->avail_out = buf_size;
+                    err = deflate(z,Z_NO_FLUSH);
+                    byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
+                } while ( z->avail_out == 0 );
             }
             break;
         }
         case MAT_T_UTF8:
         {
             data_size = 1;
-            data_tag[0]  = MAT_T_UTF8;
-            data_tag[1]  = N*data_size;
-            z->next_in   = ZLIB_BYTE_PTR(data_tag);
-            z->avail_in  = 8;
-            z->next_out  = buf;
-            z->avail_out = buf_size;
-            err = deflate(z,Z_PARTIAL_FLUSH);
-            byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
+            data_tag[0] = MAT_T_UTF8;
+            data_tag[1] = N*data_size;
+            z->next_in  = ZLIB_BYTE_PTR(data_tag);
+            z->avail_in = 8;
+            do {
+                z->next_out  = buf;
+                z->avail_out = buf_size;
+                err = deflate(z,Z_NO_FLUSH);
+                byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
+            } while ( z->avail_out == 0 );
 
             /* exit early if this is a empty data */
             if ( NULL == data || N < 1 )
                 break;
 
-            z->next_in   = data;
-            z->avail_in  = data_size*N;
+            z->next_in  = data;
+            z->avail_in = data_size*N;
             do {
                 z->next_out  = buf;
                 z->avail_out = buf_size;
-                err = deflate(z,Z_PARTIAL_FLUSH);
+                err = deflate(z,Z_NO_FLUSH);
                 byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
             } while ( z->avail_out == 0 );
             /* Add/Compress padding to pad to 8-byte boundary */
             if ( N*data_size % 8 ) {
-                z->next_in   = pad;
-                z->avail_in  = 8 - (N*data_size % 8);
-                z->next_out  = buf;
-                z->avail_out = buf_size;
-                err = deflate(z,Z_PARTIAL_FLUSH);
-                byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
+                z->next_in  = pad;
+                z->avail_in = 8 - (N*data_size % 8);
+                do {
+                    z->next_out  = buf;
+                    z->avail_out = buf_size;
+                    err = deflate(z,Z_NO_FLUSH);
+                    byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
+                } while ( z->avail_out == 0 );
             }
             break;
         }
@@ -12153,14 +12167,16 @@ WriteCompressedCharData(mat_t *mat,z_stream *z,void *data,int N,
              * a data tag
              */
             data_size = 2;
-            data_tag[0]  = MAT_T_UINT16;
-            data_tag[1]  = N*data_size;
-            z->next_in   = ZLIB_BYTE_PTR(data_tag);
-            z->avail_in  = 8;
-            z->next_out  = buf;
-            z->avail_out = buf_size;
-            err = deflate(z,Z_PARTIAL_FLUSH);
-            byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
+            data_tag[0] = MAT_T_UINT16;
+            data_tag[1] = N*data_size;
+            z->next_in  = ZLIB_BYTE_PTR(data_tag);
+            z->avail_in = 8;
+            do {
+                z->next_out  = buf;
+                z->avail_out = buf_size;
+                err = deflate(z,Z_NO_FLUSH);
+                byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
+            } while ( z->avail_out == 0 );
         }
         default:
             break;
@@ -12400,19 +12416,23 @@ WriteCompressedEmptyData(mat_t *mat,z_stream *z,int N,
             nBytes = N*data_size;
             uncomp_buf[0] = data_type;
             uncomp_buf[1] = 0;
-            z->next_out  = ZLIB_BYTE_PTR(comp_buf);
-            z->next_in   = ZLIB_BYTE_PTR(uncomp_buf);
-            z->avail_out = 32*sizeof(*comp_buf);
-            z->avail_in  = 8;
-            err = deflate(z,Z_PARTIAL_FLUSH);
-            byteswritten += fwrite(comp_buf,1,32*sizeof(*comp_buf)-z->avail_out,mat->fp);
-            for ( i = 0; i < N; i++ ) {
+            z->next_in  = ZLIB_BYTE_PTR(uncomp_buf);
+            z->avail_in = 8;
+            do {
                 z->next_out  = ZLIB_BYTE_PTR(comp_buf);
-                z->next_in   = ZLIB_BYTE_PTR(data_uncomp_buf);
                 z->avail_out = 32*sizeof(*comp_buf);
-                z->avail_in  = 8;
-                err = deflate(z,Z_PARTIAL_FLUSH);
-                byteswritten += fwrite(comp_buf,32*sizeof(*comp_buf)-z->avail_out,1,mat->fp);
+                err = deflate(z,Z_NO_FLUSH);
+                byteswritten += fwrite(comp_buf,1,32*sizeof(*comp_buf)-z->avail_out,mat->fp);
+            } while ( z->avail_out == 0 );
+            for ( i = 0; i < N; i++ ) {
+                z->next_in  = ZLIB_BYTE_PTR(data_uncomp_buf);
+                z->avail_in = 8;
+                do {
+                    z->next_out  = ZLIB_BYTE_PTR(comp_buf);
+                    z->avail_out = 32*sizeof(*comp_buf);
+                    err = deflate(z,Z_NO_FLUSH);
+                    byteswritten += fwrite(comp_buf,32*sizeof(*comp_buf)-z->avail_out,1,mat->fp);
+                } while ( z->avail_out == 0 );
             }
             break;
         }
@@ -12944,35 +12964,39 @@ WriteCompressedData(mat_t *mat,z_stream *z,void *data,int N,
 
     data_size = Mat_SizeOf(data_type);
 
-    data_tag[0]  = data_type;
-    data_tag[1]  = data_size*N;
-    z->next_in   = ZLIB_BYTE_PTR(data_tag);
-    z->avail_in  = 8;
-    z->next_out  = buf;
-    z->avail_out = buf_size;
-    err = deflate(z,Z_PARTIAL_FLUSH);
-    byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
+    data_tag[0] = data_type;
+    data_tag[1] = data_size*N;
+    z->next_in  = ZLIB_BYTE_PTR(data_tag);
+    z->avail_in = 8;
+    do {
+        z->next_out  = buf;
+        z->avail_out = buf_size;
+        err = deflate(z,Z_NO_FLUSH);
+        byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
+    } while ( z->avail_out == 0 );
 
     /* exit early if this is a empty data */
     if ( NULL == data || N < 1 )
         return byteswritten;
 
-    z->next_in   = data;
-    z->avail_in  = N*data_size;
+    z->next_in  = data;
+    z->avail_in = N*data_size;
     do {
         z->next_out  = buf;
         z->avail_out = buf_size;
-        err = deflate(z,Z_PARTIAL_FLUSH);
+        err = deflate(z,Z_NO_FLUSH);
         byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
     } while ( z->avail_out == 0 );
     /* Add/Compress padding to pad to 8-byte boundary */
     if ( N*data_size % 8 ) {
-        z->next_in   = pad;
-        z->avail_in  = 8 - (N*data_size % 8);
-        z->next_out  = buf;
-        z->avail_out = buf_size;
-        err = deflate(z,Z_PARTIAL_FLUSH);
-        byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
+        z->next_in  = pad;
+        z->avail_in = 8 - (N*data_size % 8);
+        do {
+            z->next_out  = buf;
+            z->avail_out = buf_size;
+            err = deflate(z,Z_NO_FLUSH);
+            byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
+        } while ( z->avail_out == 0 );
     }
     nBytes = byteswritten;
     return nBytes;
@@ -14015,13 +14039,15 @@ WriteCompressedCellArrayField(mat_t *mat,matvar_t *matvar,z_stream *z)
 
     uncomp_buf[0] = MAT_T_MATRIX;
     uncomp_buf[1] = (int)GetCellArrayFieldBufSize(matvar);
-    z->next_out  = ZLIB_BYTE_PTR(comp_buf);
-    z->next_in   = ZLIB_BYTE_PTR(uncomp_buf);
-    z->avail_out = buf_size*sizeof(*comp_buf);
-    z->avail_in  = 8;
-    err = deflate(z,Z_PARTIAL_FLUSH);
-    byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
-        mat->fp);
+    z->next_in  = ZLIB_BYTE_PTR(uncomp_buf);
+    z->avail_in = 8;
+    do {
+        z->next_out  = ZLIB_BYTE_PTR(comp_buf);
+        z->avail_out = buf_size*sizeof(*comp_buf);
+        err = deflate(z,Z_NO_FLUSH);
+        byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
+            mat->fp);
+    } while ( z->avail_out == 0 );
     uncomp_buf[0] = array_flags_type;
     uncomp_buf[1] = array_flags_size;
     uncomp_buf[2] = array_flags;
@@ -14041,23 +14067,27 @@ WriteCompressedCellArrayField(mat_t *mat,matvar_t *matvar,z_stream *z)
         i++;
     }
 
-    z->next_out  = ZLIB_BYTE_PTR(comp_buf);
-    z->next_in   = ZLIB_BYTE_PTR(uncomp_buf);
-    z->avail_out = buf_size*sizeof(*comp_buf);
-    z->avail_in  = (6+i)*sizeof(*uncomp_buf);
-    err = deflate(z,Z_PARTIAL_FLUSH);
-    byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
-        mat->fp);
+    z->next_in  = ZLIB_BYTE_PTR(uncomp_buf);
+    z->avail_in = (6+i)*sizeof(*uncomp_buf);
+    do {
+        z->next_out  = ZLIB_BYTE_PTR(comp_buf);
+        z->avail_out = buf_size*sizeof(*comp_buf);
+        err = deflate(z,Z_NO_FLUSH);
+        byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
+            mat->fp);
+    } while ( z->avail_out == 0 );
     /* Name of variable */
     uncomp_buf[0] = array_name_type;
     uncomp_buf[1] = 0;
-    z->next_out  = ZLIB_BYTE_PTR(comp_buf);
-    z->next_in   = ZLIB_BYTE_PTR(uncomp_buf);
-    z->avail_out = buf_size*sizeof(*comp_buf);
-    z->avail_in  = 8;
-    err = deflate(z,Z_PARTIAL_FLUSH);
-    byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
-        mat->fp);
+    z->next_in  = ZLIB_BYTE_PTR(uncomp_buf);
+    z->avail_in = 8;
+    do {
+        z->next_out  = ZLIB_BYTE_PTR(comp_buf);
+        z->avail_out = buf_size*sizeof(*comp_buf);
+        err = deflate(z,Z_NO_FLUSH);
+        byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
+            mat->fp);
+    } while ( z->avail_out == 0 );
 
     matvar->internal->datapos = ftell(mat->fp);
     switch ( matvar->class_type ) {
@@ -14127,13 +14157,15 @@ WriteCompressedCellArrayField(mat_t *mat,matvar_t *matvar,z_stream *z)
                 uncomp_buf[1] = 1;
                 uncomp_buf[2] = array_name_type;
                 uncomp_buf[3] = 0;
-                z->next_out  = ZLIB_BYTE_PTR(comp_buf);
-                z->next_in   = ZLIB_BYTE_PTR(uncomp_buf);
-                z->avail_out = buf_size*sizeof(*comp_buf);
-                z->avail_in  = 16;
-                err = deflate(z,Z_PARTIAL_FLUSH);
-                byteswritten += fwrite(comp_buf,1,buf_size*
-                    sizeof(*comp_buf)-z->avail_out,mat->fp);
+                z->next_in  = ZLIB_BYTE_PTR(uncomp_buf);
+                z->avail_in = 16;
+                do {
+                    z->next_out  = ZLIB_BYTE_PTR(comp_buf);
+                    z->avail_out = buf_size*sizeof(*comp_buf);
+                    err = deflate(z,Z_NO_FLUSH);
+                    byteswritten += fwrite(comp_buf,1,buf_size*
+                        sizeof(*comp_buf)-z->avail_out,mat->fp);
+                } while ( z->avail_out == 0 );
                 break;
             }
 
@@ -14154,24 +14186,28 @@ WriteCompressedCellArrayField(mat_t *mat,matvar_t *matvar,z_stream *z)
             uncomp_buf[3] = nfields*fieldname_size;
 
             padzero = calloc(fieldname_size,1);
-            z->next_out  = ZLIB_BYTE_PTR(comp_buf);
-            z->next_in   = ZLIB_BYTE_PTR(uncomp_buf);
-            z->avail_out = buf_size*sizeof(*comp_buf);
-            z->avail_in  = 16;
-            err = deflate(z,Z_PARTIAL_FLUSH);
-            byteswritten += fwrite(comp_buf,1,
+            z->next_in  = ZLIB_BYTE_PTR(uncomp_buf);
+            z->avail_in = 16;
+            do {
+                z->next_out  = ZLIB_BYTE_PTR(comp_buf);
+                z->avail_out = buf_size*sizeof(*comp_buf);
+                err = deflate(z,Z_NO_FLUSH);
+                byteswritten += fwrite(comp_buf,1,
                     buf_size*sizeof(*comp_buf)-z->avail_out,mat->fp);
+            } while ( z->avail_out == 0 );
             for ( i = 0; i < nfields; i++ ) {
                 memset(padzero,'\0',fieldname_size);
                 memcpy(padzero,matvar->internal->fieldnames[i],
-                       strlen(matvar->internal->fieldnames[i]));
-                z->next_out  = ZLIB_BYTE_PTR(comp_buf);
-                z->next_in   = ZLIB_BYTE_PTR(padzero);
-                z->avail_out = buf_size*sizeof(*comp_buf);
-                z->avail_in  = fieldname_size;
-                err = deflate(z,Z_PARTIAL_FLUSH);
-                byteswritten += fwrite(comp_buf,1,
+                    strlen(matvar->internal->fieldnames[i]));
+                z->next_in  = ZLIB_BYTE_PTR(padzero);
+                z->avail_in = fieldname_size;
+                do {
+                    z->next_out  = ZLIB_BYTE_PTR(comp_buf);
+                    z->avail_out = buf_size*sizeof(*comp_buf);
+                    err = deflate(z,Z_NO_FLUSH);
+                    byteswritten += fwrite(comp_buf,1,
                         buf_size*sizeof(*comp_buf)-z->avail_out,mat->fp);
+                } while ( z->avail_out == 0 );
             }
             free(padzero);
             for ( i = 0; i < nmemb*nfields; i++ )
@@ -14466,13 +14502,15 @@ WriteCompressedStructField(mat_t *mat,matvar_t *matvar,z_stream *z)
 
     uncomp_buf[0] = MAT_T_MATRIX;
     uncomp_buf[1] = (int)GetStructFieldBufSize(matvar);
-    z->next_out  = ZLIB_BYTE_PTR(comp_buf);
-    z->next_in   = ZLIB_BYTE_PTR(uncomp_buf);
-    z->avail_out = buf_size*sizeof(*comp_buf);
-    z->avail_in  = 8;
-    err = deflate(z,Z_PARTIAL_FLUSH);
-    byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
-        mat->fp);
+    z->next_in  = ZLIB_BYTE_PTR(uncomp_buf);
+    z->avail_in = 8;
+    do {
+        z->next_out  = ZLIB_BYTE_PTR(comp_buf);
+        z->avail_out = buf_size*sizeof(*comp_buf);
+        err = deflate(z,Z_NO_FLUSH);
+        byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
+            mat->fp);
+    } while ( z->avail_out == 0 );
     uncomp_buf[0] = array_flags_type;
     uncomp_buf[1] = array_flags_size;
     uncomp_buf[2] = array_flags;
@@ -14492,23 +14530,27 @@ WriteCompressedStructField(mat_t *mat,matvar_t *matvar,z_stream *z)
         i++;
     }
 
-    z->next_out  = ZLIB_BYTE_PTR(comp_buf);
-    z->next_in   = ZLIB_BYTE_PTR(uncomp_buf);
-    z->avail_out = buf_size*sizeof(*comp_buf);
-    z->avail_in  = (6+i)*sizeof(*uncomp_buf);
-    err = deflate(z,Z_PARTIAL_FLUSH);
-    byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
-        mat->fp);
+    z->next_in  = ZLIB_BYTE_PTR(uncomp_buf);
+    z->avail_in = (6+i)*sizeof(*uncomp_buf);
+    do {
+        z->next_out  = ZLIB_BYTE_PTR(comp_buf);
+        z->avail_out = buf_size*sizeof(*comp_buf);
+        err = deflate(z,Z_NO_FLUSH);
+        byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
+            mat->fp);
+    } while ( z->avail_out == 0 );
     /* Name of variable */
     uncomp_buf[0] = array_name_type;
     uncomp_buf[1] = 0;
-    z->next_out  = ZLIB_BYTE_PTR(comp_buf);
-    z->next_in   = ZLIB_BYTE_PTR(uncomp_buf);
-    z->avail_out = buf_size*sizeof(*comp_buf);
-    z->avail_in  = 8;
-    err = deflate(z,Z_PARTIAL_FLUSH);
-    byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
-        mat->fp);
+    z->next_in  = ZLIB_BYTE_PTR(uncomp_buf);
+    z->avail_in = 8;
+    do {
+        z->next_out  = ZLIB_BYTE_PTR(comp_buf);
+        z->avail_out = buf_size*sizeof(*comp_buf);
+        err = deflate(z,Z_NO_FLUSH);
+        byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
+            mat->fp);
+    } while ( z->avail_out == 0 );
 
     matvar->internal->datapos = ftell(mat->fp);
     switch ( matvar->class_type ) {
@@ -14578,13 +14620,15 @@ WriteCompressedStructField(mat_t *mat,matvar_t *matvar,z_stream *z)
                 uncomp_buf[1] = 1;
                 uncomp_buf[2] = array_name_type;
                 uncomp_buf[3] = 0;
-                z->next_out  = ZLIB_BYTE_PTR(comp_buf);
-                z->next_in   = ZLIB_BYTE_PTR(uncomp_buf);
-                z->avail_out = buf_size*sizeof(*comp_buf);
-                z->avail_in  = 16;
-                err = deflate(z,Z_PARTIAL_FLUSH);
-                byteswritten += fwrite(comp_buf,1,buf_size*
-                    sizeof(*comp_buf)-z->avail_out,mat->fp);
+                z->next_in  = ZLIB_BYTE_PTR(uncomp_buf);
+                z->avail_in = 16;
+                do {
+                    z->next_out  = ZLIB_BYTE_PTR(comp_buf);
+                    z->avail_out = buf_size*sizeof(*comp_buf);
+                    err = deflate(z,Z_NO_FLUSH);
+                    byteswritten += fwrite(comp_buf,1,buf_size*
+                        sizeof(*comp_buf)-z->avail_out,mat->fp);
+                } while ( z->avail_out == 0 );
                 break;
             }
             for ( i = 0; i < nfields; i++ ) {
@@ -14602,24 +14646,28 @@ WriteCompressedStructField(mat_t *mat,matvar_t *matvar,z_stream *z)
             uncomp_buf[3] = nfields*fieldname_size;
 
             padzero = calloc(fieldname_size,1);
-            z->next_out  = ZLIB_BYTE_PTR(comp_buf);
-            z->next_in   = ZLIB_BYTE_PTR(uncomp_buf);
-            z->avail_out = buf_size*sizeof(*comp_buf);
-            z->avail_in  = 16;
-            err = deflate(z,Z_PARTIAL_FLUSH);
-            byteswritten += fwrite(comp_buf,1,
+            z->next_in  = ZLIB_BYTE_PTR(uncomp_buf);
+            z->avail_in = 16;
+            do {
+                z->next_out  = ZLIB_BYTE_PTR(comp_buf);
+                z->avail_out = buf_size*sizeof(*comp_buf);
+                err = deflate(z,Z_NO_FLUSH);
+                byteswritten += fwrite(comp_buf,1,
                     buf_size*sizeof(*comp_buf)-z->avail_out,mat->fp);
+            } while ( z->avail_out == 0 );
             for ( i = 0; i < nfields; i++ ) {
                 size_t len = strlen(matvar->internal->fieldnames[i]);
                 memset(padzero,'\0',fieldname_size);
                 memcpy(padzero,matvar->internal->fieldnames[i],len);
-                z->next_out  = ZLIB_BYTE_PTR(comp_buf);
-                z->next_in   = ZLIB_BYTE_PTR(padzero);
-                z->avail_out = buf_size*sizeof(*comp_buf);
-                z->avail_in  = fieldname_size;
-                err = deflate(z,Z_PARTIAL_FLUSH);
-                byteswritten += fwrite(comp_buf,1,
+                z->next_in  = ZLIB_BYTE_PTR(padzero);
+                z->avail_in = fieldname_size;
+                do {
+                    z->next_out  = ZLIB_BYTE_PTR(comp_buf);
+                    z->avail_out = buf_size*sizeof(*comp_buf);
+                    err = deflate(z,Z_NO_FLUSH);
+                    byteswritten += fwrite(comp_buf,1,
                         buf_size*sizeof(*comp_buf)-z->avail_out,mat->fp);
+                } while ( z->avail_out == 0 );
             }
             free(padzero);
             for ( i = 0; i < nmemb*nfields; i++ )
@@ -14760,12 +14808,14 @@ Mat_WriteCompressedEmptyVariable5(mat_t *mat,const char *name,int rank,
 
     uncomp_buf[0] = MAT_T_MATRIX;
     uncomp_buf[1] = (int)GetEmptyMatrixMaxBufSize(name,rank);
-    z->next_out  = ZLIB_BYTE_PTR(comp_buf);
-    z->next_in   = ZLIB_BYTE_PTR(uncomp_buf);
-    z->avail_out = buf_size_bytes;
-    z->avail_in  = 8;
-    err = deflate(z,Z_PARTIAL_FLUSH);
-    byteswritten += fwrite(comp_buf,1,buf_size_bytes-z->avail_out,mat->fp);
+    z->next_in  = ZLIB_BYTE_PTR(uncomp_buf);
+    z->avail_in = 8;
+    do {
+        z->next_out  = ZLIB_BYTE_PTR(comp_buf);
+        z->avail_out = buf_size_bytes;
+        err = deflate(z,Z_NO_FLUSH);
+        byteswritten += fwrite(comp_buf,1,buf_size_bytes-z->avail_out,mat->fp);
+    } while ( z->avail_out == 0 );
     uncomp_buf[0] = array_flags_type;
     uncomp_buf[1] = array_flags_size;
     uncomp_buf[2] = array_flags;
@@ -14785,22 +14835,26 @@ Mat_WriteCompressedEmptyVariable5(mat_t *mat,const char *name,int rank,
         i++;
     }
 
-    z->next_out  = ZLIB_BYTE_PTR(comp_buf);
-    z->next_in   = ZLIB_BYTE_PTR(uncomp_buf);
-    z->avail_out = buf_size_bytes;
-    z->avail_in  = (6+i)*sizeof(*uncomp_buf);
-    err = deflate(z,Z_PARTIAL_FLUSH);
-    byteswritten += fwrite(comp_buf,1,buf_size_bytes-z->avail_out,mat->fp);
+    z->next_in  = ZLIB_BYTE_PTR(uncomp_buf);
+    z->avail_in = (6+i)*sizeof(*uncomp_buf);
+    do {
+        z->next_out  = ZLIB_BYTE_PTR(comp_buf);
+        z->avail_out = buf_size_bytes;
+        err = deflate(z,Z_NO_FLUSH);
+        byteswritten += fwrite(comp_buf,1,buf_size_bytes-z->avail_out,mat->fp);
+    } while ( z->avail_out == 0 );
     /* Name of variable */
     if ( NULL == name ) {
         uncomp_buf[0] = array_name_type;
         uncomp_buf[1] = 0;
-        z->next_out  = ZLIB_BYTE_PTR(comp_buf);
-        z->next_in   = ZLIB_BYTE_PTR(uncomp_buf);
-        z->avail_out = buf_size_bytes;
-        z->avail_in  = 8;
-        err = deflate(z,Z_PARTIAL_FLUSH);
-        byteswritten += fwrite(comp_buf,1,buf_size_bytes-z->avail_out,mat->fp);
+        z->next_in  = ZLIB_BYTE_PTR(uncomp_buf);
+        z->avail_in = 8;
+        do {
+            z->next_out  = ZLIB_BYTE_PTR(comp_buf);
+            z->avail_out = buf_size_bytes;
+            err = deflate(z,Z_NO_FLUSH);
+            byteswritten += fwrite(comp_buf,1,buf_size_bytes-z->avail_out,mat->fp);
+        } while ( z->avail_out == 0 );
     } else {
         if ( strlen(name) <= 4 ) {
             mat_int16_t array_name_len = (mat_int16_t)strlen(name);
@@ -14812,13 +14866,15 @@ Mat_WriteCompressedEmptyVariable5(mat_t *mat,const char *name,int rank,
             if ( array_name_len % 4 )
                 array_name_len += 4-(array_name_len % 4);
 
-            z->next_out  = ZLIB_BYTE_PTR(comp_buf);
-            z->next_in   = ZLIB_BYTE_PTR(uncomp_buf);
-            z->avail_out = buf_size_bytes;
-            z->avail_in  = 8;
-            err = deflate(z,Z_PARTIAL_FLUSH);
-            byteswritten += fwrite(comp_buf,1,buf_size_bytes-z->avail_out,
-                                   mat->fp);
+            z->next_in  = ZLIB_BYTE_PTR(uncomp_buf);
+            z->avail_in = 8;
+            do {
+                z->next_out  = ZLIB_BYTE_PTR(comp_buf);
+                z->avail_out = buf_size_bytes;
+                err = deflate(z,Z_NO_FLUSH);
+                byteswritten += fwrite(comp_buf,1,buf_size_bytes-z->avail_out,
+                    mat->fp);
+            } while ( z->avail_out == 0 );
         } else {
             mat_int32_t array_name_len = (mat_int32_t)strlen(name);
             mat_int32_t array_name_type = MAT_T_INT8;
@@ -14829,13 +14885,15 @@ Mat_WriteCompressedEmptyVariable5(mat_t *mat,const char *name,int rank,
             memcpy(uncomp_buf+2,name,array_name_len);
             if ( array_name_len % 8 )
                 array_name_len += 8-(array_name_len % 8);
-            z->next_out  = ZLIB_BYTE_PTR(comp_buf);
-            z->next_in   = ZLIB_BYTE_PTR(uncomp_buf);
-            z->avail_out = buf_size_bytes;
-            z->avail_in  = 8+array_name_len;
-            err = deflate(z,Z_PARTIAL_FLUSH);
-            byteswritten += fwrite(comp_buf,1,buf_size_bytes-z->avail_out,
-                                   mat->fp);
+            z->next_in  = ZLIB_BYTE_PTR(uncomp_buf);
+            z->avail_in = 8+array_name_len;
+            do {
+                z->next_out  = ZLIB_BYTE_PTR(comp_buf);
+                z->avail_out = buf_size_bytes;
+                err = deflate(z,Z_NO_FLUSH);
+                byteswritten += fwrite(comp_buf,1,buf_size_bytes-z->avail_out,
+                    mat->fp);
+            } while ( z->avail_out == 0 );
         }
     }
 
@@ -16860,13 +16918,15 @@ Mat_VarWrite5(mat_t *mat,matvar_t *matvar,int compress)
 
         uncomp_buf[0] = MAT_T_MATRIX;
         uncomp_buf[1] = (int)GetMatrixMaxBufSize(matvar);
-        matvar->internal->z->next_out  = ZLIB_BYTE_PTR(comp_buf);
-        matvar->internal->z->next_in   = ZLIB_BYTE_PTR(uncomp_buf);
-        matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
-        matvar->internal->z->avail_in  = 8;
-        err = deflate(matvar->internal->z,Z_PARTIAL_FLUSH);
-        byteswritten += fwrite(comp_buf,1,
-            buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
+        matvar->internal->z->next_in  = ZLIB_BYTE_PTR(uncomp_buf);
+        matvar->internal->z->avail_in = 8;
+        do {
+            matvar->internal->z->next_out  = ZLIB_BYTE_PTR(comp_buf);
+            matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
+            err = deflate(matvar->internal->z,Z_NO_FLUSH);
+            byteswritten += fwrite(comp_buf,1,
+                buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
+        } while ( matvar->internal->z->avail_out == 0 );
         uncomp_buf[0] = array_flags_type;
         uncomp_buf[1] = array_flags_size;
         uncomp_buf[2] = array_flags;
@@ -16886,13 +16946,15 @@ Mat_VarWrite5(mat_t *mat,matvar_t *matvar,int compress)
             i++;
         }
 
-        matvar->internal->z->next_out  = ZLIB_BYTE_PTR(comp_buf);
-        matvar->internal->z->next_in   = ZLIB_BYTE_PTR(uncomp_buf);
-        matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
-        matvar->internal->z->avail_in  = (6+i)*sizeof(*uncomp_buf);
-        err = deflate(matvar->internal->z,Z_PARTIAL_FLUSH);
-        byteswritten += fwrite(comp_buf,1,
+        matvar->internal->z->next_in  = ZLIB_BYTE_PTR(uncomp_buf);
+        matvar->internal->z->avail_in = (6+i)*sizeof(*uncomp_buf);
+        do {
+            matvar->internal->z->next_out  = ZLIB_BYTE_PTR(comp_buf);
+            matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
+            err = deflate(matvar->internal->z,Z_NO_FLUSH);
+            byteswritten += fwrite(comp_buf,1,
                 buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
+        } while ( matvar->internal->z->avail_out == 0 );
         /* Name of variable */
         if ( strlen(matvar->name) <= 4 ) {
             mat_int16_t array_name_len = (mat_int16_t)strlen(matvar->name);
@@ -16904,13 +16966,15 @@ Mat_VarWrite5(mat_t *mat,matvar_t *matvar,int compress)
             if ( array_name_len % 4 )
                 array_name_len += 4-(array_name_len % 4);
 
-            matvar->internal->z->next_out  = ZLIB_BYTE_PTR(comp_buf);
-            matvar->internal->z->next_in   = ZLIB_BYTE_PTR(uncomp_buf);
-            matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
-            matvar->internal->z->avail_in  = 8;
-            err = deflate(matvar->internal->z,Z_PARTIAL_FLUSH);
-            byteswritten += fwrite(comp_buf,1,
+            matvar->internal->z->next_in  = ZLIB_BYTE_PTR(uncomp_buf);
+            matvar->internal->z->avail_in = 8;
+            do {
+                matvar->internal->z->next_out  = ZLIB_BYTE_PTR(comp_buf);
+                matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
+                err = deflate(matvar->internal->z,Z_NO_FLUSH);
+                byteswritten += fwrite(comp_buf,1,
                     buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
+            } while ( matvar->internal->z->avail_out == 0 );
         } else {
             mat_int32_t array_name_len = (mat_int32_t)strlen(matvar->name);
             mat_int32_t array_name_type = MAT_T_INT8;
@@ -16921,13 +16985,15 @@ Mat_VarWrite5(mat_t *mat,matvar_t *matvar,int compress)
             memcpy(uncomp_buf+2,matvar->name,array_name_len);
             if ( array_name_len % 8 )
                 array_name_len += 8-(array_name_len % 8);
-            matvar->internal->z->next_out  = ZLIB_BYTE_PTR(comp_buf);
-            matvar->internal->z->next_in   = ZLIB_BYTE_PTR(uncomp_buf);
-            matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
-            matvar->internal->z->avail_in  = 8+array_name_len;
-            err = deflate(matvar->internal->z,Z_PARTIAL_FLUSH);
-            byteswritten += fwrite(comp_buf,1,
+            matvar->internal->z->next_in  = ZLIB_BYTE_PTR(uncomp_buf);
+            matvar->internal->z->avail_in = 8+array_name_len;
+            do {
+                matvar->internal->z->next_out  = ZLIB_BYTE_PTR(comp_buf);
+                matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
+                err = deflate(matvar->internal->z,Z_NO_FLUSH);
+                byteswritten += fwrite(comp_buf,1,
                     buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
+            } while ( matvar->internal->z->avail_out == 0 );
         }
         matvar->internal->datapos = ftell(mat->fp);
         switch ( matvar->class_type ) {
@@ -16996,13 +17062,15 @@ Mat_VarWrite5(mat_t *mat,matvar_t *matvar,int compress)
                     uncomp_buf[1] = 1;
                     uncomp_buf[2] = array_name_type;
                     uncomp_buf[3] = 0;
-                    matvar->internal->z->next_out  = ZLIB_BYTE_PTR(comp_buf);
-                    matvar->internal->z->next_in   = ZLIB_BYTE_PTR(uncomp_buf);
-                    matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
-                    matvar->internal->z->avail_in  = 16;
-                    err = deflate(matvar->internal->z,Z_PARTIAL_FLUSH);
-                    byteswritten += fwrite(comp_buf,1,buf_size*
-                        sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
+                    matvar->internal->z->next_in  = ZLIB_BYTE_PTR(uncomp_buf);
+                    matvar->internal->z->avail_in = 16;
+                    do {
+                        matvar->internal->z->next_out  = ZLIB_BYTE_PTR(comp_buf);
+                        matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
+                        err = deflate(matvar->internal->z,Z_NO_FLUSH);
+                        byteswritten += fwrite(comp_buf,1,buf_size*
+                            sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
+                    } while ( matvar->internal->z->avail_out == 0 );
                     break;
                 }
                 nfields = matvar->internal->num_fields;
@@ -17021,25 +17089,29 @@ Mat_VarWrite5(mat_t *mat,matvar_t *matvar,int compress)
                 uncomp_buf[3] = nfields*fieldname_size;
 
                 padzero = calloc(fieldname_size,1);
-                matvar->internal->z->next_out  = ZLIB_BYTE_PTR(comp_buf);
-                matvar->internal->z->next_in   = ZLIB_BYTE_PTR(uncomp_buf);
-                matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
-                matvar->internal->z->avail_in  = 16;
-                err = deflate(matvar->internal->z,Z_PARTIAL_FLUSH);
-                byteswritten += fwrite(comp_buf,1,
-                    buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
+                matvar->internal->z->next_in  = ZLIB_BYTE_PTR(uncomp_buf);
+                matvar->internal->z->avail_in = 16;
+                do {
+                    matvar->internal->z->next_out  = ZLIB_BYTE_PTR(comp_buf);
+                    matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
+                    err = deflate(matvar->internal->z,Z_NO_FLUSH);
+                    byteswritten += fwrite(comp_buf,1,
+                        buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
+                } while ( matvar->internal->z->avail_out == 0 );
                 for ( i = 0; i < nfields; i++ ) {
                     size_t len = strlen(matvar->internal->fieldnames[i]);
                     memset(padzero,'\0',fieldname_size);
                     memcpy(padzero,matvar->internal->fieldnames[i],len);
-                    matvar->internal->z->next_out  = ZLIB_BYTE_PTR(comp_buf);
-                    matvar->internal->z->next_in   = ZLIB_BYTE_PTR(padzero);
-                    matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
-                    matvar->internal->z->avail_in  = fieldname_size;
-                    err = deflate(matvar->internal->z,Z_PARTIAL_FLUSH);
-                    byteswritten += fwrite(comp_buf,1,
+                    matvar->internal->z->next_in  = ZLIB_BYTE_PTR(padzero);
+                    matvar->internal->z->avail_in = fieldname_size;
+                    do {
+                        matvar->internal->z->next_out  = ZLIB_BYTE_PTR(comp_buf);
+                        matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
+                        err = deflate(matvar->internal->z,Z_NO_FLUSH);
+                        byteswritten += fwrite(comp_buf,1,
                             buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,
                             mat->fp);
+                    } while ( matvar->internal->z->avail_out == 0 );
                 }
                 free(padzero);
                 for ( i = 0; i < nmemb*nfields; i++ )
@@ -17072,22 +17144,15 @@ Mat_VarWrite5(mat_t *mat,matvar_t *matvar,int compress)
             case MAT_C_OBJECT:
                 break;
         }
-        matvar->internal->z->avail_in  = 0;
-        matvar->internal->z->next_in   = NULL;
-        matvar->internal->z->next_out  = ZLIB_BYTE_PTR(comp_buf);
-        matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
-
-        err = deflate(matvar->internal->z,Z_FINISH);
-        byteswritten += fwrite(comp_buf,1,
-            buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
-        while ( err != Z_STREAM_END && !matvar->internal->z->avail_out ) {
+        matvar->internal->z->next_in  = NULL;
+        matvar->internal->z->avail_in = 0;
+        do {
             matvar->internal->z->next_out  = ZLIB_BYTE_PTR(comp_buf);
             matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
-
             err = deflate(matvar->internal->z,Z_FINISH);
             byteswritten += fwrite(comp_buf,1,
                 buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
-        }
+        } while ( err != Z_STREAM_END && matvar->internal->z->avail_out == 0 );
         /* End the compression and set to NULL so Mat_VarFree doesn't try
          * to free matvar->internal->z with inflateEnd
          */
@@ -17310,12 +17375,16 @@ WriteInfo5(mat_t *mat, matvar_t *matvar)
 
         uncomp_buf[0] = MAT_T_MATRIX;
         uncomp_buf[1] = 448;
-        matvar->internal->z->next_out  = comp_buf;
-        matvar->internal->z->next_in   = uncomp_buf;
-        matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
-        matvar->internal->z->avail_in  = 8;
-        err = deflate(matvar->internal->z,Z_SYNC_FLUSH);
-        byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
+        matvar->internal->z->next_in  = uncomp_buf;
+        matvar->internal->z->avail_in = 8;
+        do {
+            matvar->internal->z->next_out  = comp_buf;
+            matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
+            err = deflate(matvar->internal->z,Z_NO_FLUSH);
+            byteswritten += fwrite(comp_buf,1,
+                buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,
+                mat->fp);
+        } while ( matvar->internal->z->avail_out == 0 );
         uncomp_buf[0] = array_flags_type;
         uncomp_buf[1] = array_flags_size;
         uncomp_buf[2] = array_flags;
@@ -17333,12 +17402,16 @@ WriteInfo5(mat_t *mat, matvar_t *matvar)
         if ( matvar->rank % 2 != 0 )
             uncomp_buf[6+i] = pad4;
 
-        matvar->internal->z->next_out  = comp_buf;
-        matvar->internal->z->next_in   = uncomp_buf;
-        matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
-        matvar->internal->z->avail_in  = (6+i)*sizeof(*uncomp_buf);
-        err = deflate(matvar->internal->z,Z_PARTIAL_FLUSH);
-        byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
+        matvar->internal->z->next_in  = uncomp_buf;
+        matvar->internal->z->avail_in = (6+i)*sizeof(*uncomp_buf);
+        do {
+            matvar->internal->z->next_out  = comp_buf;
+            matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
+            err = deflate(matvar->internal->z,Z_NO_FLUSH);
+            byteswritten += fwrite(comp_buf,1,
+                buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,
+                mat->fp);
+        } while ( matvar->internal->z->avail_out == 0 );
         /* Name of variable */
         if ( strlen(matvar->name) <= 4 ) {
 #if 0
@@ -17348,12 +17421,16 @@ WriteInfo5(mat_t *mat, matvar_t *matvar)
             uncomp_buf[0] = (array_name_type << 16) | array_name_len;
             memcpy(uncomp_buf+1,matvar->name,array_name_len);
 
-            matvar->internal->z->next_out  = comp_buf;
-            matvar->internal->z->next_in   = uncomp_buf;
-            matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
-            matvar->internal->z->avail_in  = 8;
-            err = deflate(matvar->internal->z,Z_PARTIAL_FLUSH);
-            byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
+            matvar->internal->z->next_in  = uncomp_buf;
+            matvar->internal->z->avail_in = 8;
+            do {
+                matvar->internal->z->next_out  = comp_buf;
+                matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
+                err = deflate(matvar->internal->z,Z_NO_FLUSH);
+                byteswritten += fwrite(comp_buf,1,
+                    buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,
+                    mat->fp);
+            } while ( matvar->internal->z->avail_out == 0 );
         } else {
 #endif
             mat_int32_t array_name_len = (mat_int32_t)strlen(matvar->name);
@@ -17364,12 +17441,15 @@ WriteInfo5(mat_t *mat, matvar_t *matvar)
             memcpy(uncomp_buf+2,matvar->name,array_name_len);
             if ( array_name_len % 8 )
                 array_name_len += array_name_len % 8;
-            matvar->internal->z->next_out  = comp_buf;
             matvar->internal->z->next_in   = uncomp_buf;
-            matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
             matvar->internal->z->avail_in  = 8+array_name_len;
-            err = deflate(matvar->internal->z,Z_FULL_FLUSH);
-            byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
+            do {
+                matvar->internal->z->next_out  = comp_buf;
+                matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
+                err = deflate(matvar->internal->z,Z_NO_FLUSH);
+                byteswritten += fwrite(comp_buf,1,
+                    buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
+            } while ( matvar->internal->z->avail_out == 0 );
         }
         matvar->internal->datapos = ftell(mat->fp);
         deflateCopy(&z_save,matvar->internal->z);
@@ -17396,16 +17476,19 @@ WriteInfo5(mat_t *mat, matvar_t *matvar)
 #endif
                 break;
         }
-        matvar->internal->z->next_out  = comp_buf;
-        matvar->internal->z->next_in   = NULL;
-        matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
-        matvar->internal->z->avail_in  = 0;
-
-        err = deflate(matvar->internal->z,Z_FINISH);
-        byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
-                if ( byteswritten % 8 )
-                    for ( i = byteswritten % 8; i < 8; i++ )
-                        fwrite(&pad1,1,1,mat->fp);
+        matvar->internal->z->next_in  = NULL;
+        matvar->internal->z->avail_in = 0;
+        do {
+            matvar->internal->z->next_out  = comp_buf;
+            matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
+            err = deflate(matvar->internal->z,Z_FINISH);
+            byteswritten += fwrite(comp_buf,1,
+                buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,
+                mat->fp);
+        } while ( err != Z_STREAM_END && matvar->internal->z->avail_out == 0 );
+        if ( byteswritten % 8 )
+            for ( i = byteswritten % 8; i < 8; i++ )
+                fwrite(&pad1,1,1,mat->fp);
         fprintf(stderr,"deflate Z_FINISH: err = %d,byteswritten = %u\n",err,byteswritten);
 
         err = deflateEnd(matvar->internal->z);