code: def CompressStream(in_stream, length=None, compresslevel=2,
                     chunksize=16777216):
  
      
      in_read = 0
      in_exhausted = False
      out_stream = StreamingBuffer()
      with gzip.GzipFile(mode='wb',
                         fileobj=out_stream,
                         compresslevel=compresslevel) as compress_stream:
          # Read until we've written at least length bytes to the output stream.
          while not length or out_stream.length < length:
              data = in_stream.read(chunksize)
              data_length = len(data)
              compress_stream.write(data)
              in_read += data_length
              # If we read less than requested, the stream is exhausted.
              if data_length < chunksize:
                  in_exhausted = True
                  break
      return out_stream, in_read, in_exhausted
docs: Compresses an input stream into a file-like buffer.
  
      This reads from the input stream until either we've stored at least length
      compressed bytes, or the input stream has been exhausted.
  
      This supports streams of unknown size.
  
      Args:
          in_stream: The input stream to read from.
          length: The target number of compressed bytes to buffer in the output
              stream. If length is none, the input stream will be compressed
              until it's exhausted.
  
              The actual length of the output buffer can vary from the target.
              If the input stream is exhaused, the output buffer may be smaller
              than expected. If the data is incompressible, the maximum length
              can be exceeded by can be calculated to be:
  
                chunksize + 5 * (floor((chunksize - 1) / 16383) + 1) + 17
  
              This accounts for additional header data gzip adds. For the default
              16MiB chunksize, this results in the max size of the output buffer
              being:
  
                length + 16Mib + 5142 bytes
  
          compresslevel: Optional, defaults to 2. The desired compression level.
          chunksize: Optional, defaults to 16MiB. The chunk size used when
              reading data from the input stream to write into the output
              buffer.
  
      Returns:
          A file-like output buffer of compressed bytes, the number of bytes read
          from the input stream, and a flag denoting if the input stream was
          exhausted.