To answer my own question, it's actually the opposite.
System.IO.Compression.DeflateStream
doesn't add any extra data, it outputs a raw deflate stream with no header/metadata.
I actually had to add the ZLib header to the stream. See RFC 1950.
This answer also helped me:
What does a zlib header look like?
In detail, there must be two bytes in a zlib header:
0 1
+---+---+
|CMF|FLG|
+---+---+
First byte (CMF):
Each nibble (half-byte) of the first byte has a meaning:
bits 0 to 3 CM Compression method
bits 4 to 7 CINFO Compression info
You can see it in more detail in the answer I've mentioned above.
Second byte (FLG):
FLG (FLaGs) This flag byte is divided as follows:
bits 0 to 4 FCHECK (check bits for CMF and FLG)
bit 5 FDICT (preset dictionary)
bits 6 to 7 FLEVEL (compression level)
The most common headers are:
78 01 - No Compression/low
78 9C - Default Compression
78 DA - Best Compression
I've added the header 78 9C
before System.IO.Compression.DeflateStream
's output, but there's one more step as @Mark Adler said in the comments: we have to add an Adler-32 checksum at the end of the stream.
Edit:
In the end, I was trying to reinvent the wheel. I can use System.IO.Compression.GZipStream
in the PowerShell compression and just use inflateInit2()
in the decompression and all should be ok (thanks to @Mark Adler's suggestion).