diff --git a/sources/io.F90 b/sources/io.F90
index 9298ddd..407f961 100644
--- a/sources/io.F90
+++ b/sources/io.F90
@@ -2806,7 +2806,7 @@ module io
                               array_bytes, array_digest,                       &
                               compressed_bytes, compressed_digest)
 
-    use compression, only : get_compression, compress
+    use compression, only : get_compression, compression_bound, compress
     use hash       , only : get_hash
 
     implicit none
@@ -2841,10 +2841,11 @@ module io
 ! try to compress array and write it
 !
     if (present(compressed_bytes) .and. get_compression() > 0) then
-      allocate(buffer(array_bytes), stat = status)
+      compressed_bytes = compression_bound(array_bytes)
+      allocate(buffer(compressed_bytes), stat = status)
       if (status == 0) then
         call compress(array, array_bytes, buffer, compressed_bytes)
-        if (compressed_bytes > 0) then
+        if (compressed_bytes < array_bytes) then
           open(newunit = lun, file = fname, form = 'unformatted',              &
                               access = 'stream', status = 'replace')
           write(lun) buffer(1:compressed_bytes)