def create_gzip_file(filepath, overwrite):
 compressed_path = (filepath + '.gz')
 with open(filepath, 'rb') as uncompressed:
  gzip_compress_obj = zlib.compressobj(COMPRESSION_LEVEL, zlib.DEFLATED, WBITS)
  uncompressed_data = uncompressed.read()
  gzipped_data = gzip_compress_obj.compress(uncompressed_data)
  gzipped_data += gzip_compress_obj.flush()
  if (len(gzipped_data) >= len(uncompressed_data)):
   logger.debug(('No improvement: %s' % filepath))
   return
  with open(compressed_path, 'wb') as compressed:
   logger.debug(('Compressing: %s' % filepath))
   try:
    compressed.write(gzipped_data)
   except Exception as ex:
    logger.critical(('Gzip compression failed: %s' % ex))
  if overwrite:
   logger.debug(('Overwriting: %s with %s' % (filepath, compressed_path)))
   os.remove(filepath)
   os.rename(compressed_path, filepath)

def create_gzip_file ( filepath , overwrite ) : compressed_path = ( filepath + '.gz' ) with open ( filepath , 'rb' ) as uncompressed : gzip_compress_obj = zlib . compressobj ( COMPRESSION_LEVEL , zlib . DEFLATED , WBITS ) uncompressed_data = uncompressed . read ( ) gzipped_data = gzip_compress_obj . compress ( uncompressed_data ) gzipped_data += gzip_compress_obj . flush ( ) if ( len ( gzipped_data ) >= len ( uncompressed_data ) ) : logger . debug ( ( 'No improvement: %s' % filepath ) ) return with open ( compressed_path , 'wb' ) as compressed : logger . debug ( ( 'Compressing: %s' % filepath ) ) try : compressed . write ( gzipped_data ) except Exception as ex : logger . critical ( ( 'Gzip compression failed: %s' % ex ) ) if overwrite : logger . debug ( ( 'Overwriting: %s with %s' % ( filepath , compressed_path ) ) ) os . remove ( filepath ) os . rename ( compressed_path , filepath )
create a gzipped file in the same directory with a filepath .