From d884dfa978bbcbaa18c8f0548172e0cad111ecee Mon Sep 17 00:00:00 2001 From: Henri Sivonen Date: Sat, 17 Oct 2015 20:37:55 +0300 Subject: Avoid storing compressed data when the tagged compressed data would be longer than the uncompressed data. --- obnamlib/plugins/compression_plugin.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/obnamlib/plugins/compression_plugin.py b/obnamlib/plugins/compression_plugin.py index a9eb2d21..eba3210d 100644 --- a/obnamlib/plugins/compression_plugin.py +++ b/obnamlib/plugins/compression_plugin.py @@ -31,14 +31,21 @@ class DeflateCompressionFilter(object): def filter_write(self, data, repo, toplevel): how = self.app.settings['compress-with'] + compressed = None if how == 'deflate': - data = zlib.compress(data) + compressed = zlib.compress(data) elif how == 'gzip': if not self.warned: self.app.ts.notify("--compress-with=gzip is deprecated. " + "Use --compress-with=deflate instead") self.warned = True - data = zlib.compress(data) + compressed = zlib.compress(data) + + # If the compression result, the tag and the separator byte taken + # together are longer than the uncompressed input, let's store the + # uncompressed data to avoid waste upon transfer, storage and read. + if compressed and len(compressed) + len(self.tag) + 1 < len(data): + return compressed return data -- cgit v1.2.1