I have a flow where some data (like an image/video) gets compressed using GZip like this:
await using var outputStream = new MemoryStream();
await using var compressionStream = new GZipStream(outputStream, CompressionMode.Compress);
await compressionStream.WriteAsync(payload);
await compressionStream.FlushAsync();
outputStream.Position = 0;
return outputStream.ToArray()
The above code is not from my team but it can be changed if needed.
If I get the output into a base64 string and test decompressing it with this simple code, it works perfectly:
var bytes = Convert.FromBase64String("H4sIAAAAAAAACirOz01VKEmtKAEAAAD//w=="); // "some text"
using var ms = new MemoryStream(bytes);
using var ds = new GZipStream(ms, CompressionMode.Decompress);
using var output = new MemoryStream();
ds.CopyTo(output);
ds.Flush();
var result = output.ToArray();
However, my requirement is to get the compressed payload in a python script and decompress it before passing it to another system. I'm not at all familiar with python, so I made this very simple script:
import base64
import gzip
encodedBase64 = "H4sIAAAAAAAACirOz01VKEmtKAEAAAD//w=="
decodedBytes = base64.standard_b64decode(encodedBase64)
decompressedBytes = gzip.decompress(decodedBytes)
The above fails with: EOFError: Compressed file ended before the end-of-stream marker was reached
I have of course done research and found posts like this Q&A but nothing has helped (for example, using that answer fails with gzip.BadGzipFile: Not a gzipped file (b'\x00\x00'). Other attempts have yielded different gzip errors.