Currently .Net doesn't support the zlib data format despite the majority of the work having been done by DeflateStream. I propose a new class be added similar to GZipStream to support RFC 1950.
Personally I'm encountering the zlib format in two areas. Many games depend on zlib, and save files using this format. We also use zlib in the firmware of one of our hardware products. We save debug data and compress it with zlib. For both of these scenarios, we use C# tooling, on the desktop, to operate on these files whether it is for the purpose of reading, modifying, or writing.
Due to the lack of support in .Net I either have to use lame hacky methods, or rely on a separate third party library for the functionality. This is such a shame when .Net already provides the majority of what's needed, and it should be trivial (as far I can tell) to include this as part of .Net.
While decompression is quite simple to do by skipping the 2-6 byte header, and 4 byte CRC at the end, I have trouble with compression. I wouldn't know what the proper header should be when using DeflateStream to do the compression, not to mention having to include a computation for the Adler32 checksum.
Here's a quick untested example of what I currently have to do for decompression to give you an idea of the "lame hacky-ness": (Note: I'm not checking the checksum here. I'm assuming the data is good.)
privatebyte[]ZlibDecompress(byte[]data){using(varms=newMemoryStream())using(vards=newDeflateStream(ms,CompressionMode.Decompress))using(varmsOut=newMemoryStream()){intheaderSize=(data[1]&0b10000)==0b10000?6:2;ms.Write(data,headerSize,data.Length-4-headerSize);ms.Seek(0,SeekOrigin.Begin);ds.CopyTo(msOut);returnmsOut.ToArray();}}Proposed API
publicclassZLibStream:Stream{publicZLibStream(Streamstream,CompressionLevelcompressionLevel);publicZLibStream(Streamstream,CompressionModemode);publicZLibStream(Streamstream,CompressionLevelcompressionLevel,boolleaveOpen);publicZLibStream(Streamstream,CompressionModemode,boolleaveOpen);publicoverrideboolCanWrite{get;}publicoverrideboolCanSeek{get;}publicoverrideboolCanRead{get;}publicStreamBaseStream{get;}publicoverridelongLength{get;}publicoverridelongPosition{get;set;}publicoverridevoidCopyTo(Streamdestination,intbufferSize);publicoverrideTaskCopyToAsync(Streamdestination,intbufferSize,CancellationTokencancellationToken);publicoverrideValueTaskDisposeAsync();publicoverridevoidFlush();publicoverrideTaskFlushAsync(CancellationTokencancellationToken);publicoverrideintRead(Span<byte>buffer);publicoverrideintRead(byte[]array,intoffset,intcount);publicoverrideValueTask<int>ReadAsync(Memory<byte>buffer,CancellationTokencancellationToken=default);publicoverrideTask<int>ReadAsync(byte[]array,intoffset,intcount,CancellationTokencancellationToken);publicoverrideintReadByte();publicoverridelongSeek(longoffset,SeekOriginorigin);publicoverridevoidSetLength(longvalue);publicoverridevoidWrite(byte[]array,intoffset,intcount);publicoverridevoidWrite(ReadOnlySpan<byte>buffer);publicoverrideTaskWriteAsync(byte[]array,intoffset,intcount,CancellationTokencancellationToken);publicoverrideValueTaskWriteAsync(ReadOnlyMemory<byte>buffer,CancellationTokencancellationToken=default);protectedoverridevoidDispose(booldisposing);}
Currently .Net doesn't support the zlib data format despite the majority of the work having been done by DeflateStream. I propose a new class be added similar to GZipStream to support RFC 1950.
Personally I'm encountering the zlib format in two areas. Many games depend on zlib, and save files using this format. We also use zlib in the firmware of one of our hardware products. We save debug data and compress it with zlib. For both of these scenarios, we use C# tooling, on the desktop, to operate on these files whether it is for the purpose of reading, modifying, or writing.
Due to the lack of support in .Net I either have to use lame hacky methods, or rely on a separate third party library for the functionality. This is such a shame when .Net already provides the majority of what's needed, and it should be trivial (as far I can tell) to include this as part of .Net.
While decompression is quite simple to do by skipping the 2-6 byte header, and 4 byte CRC at the end, I have trouble with compression. I wouldn't know what the proper header should be when using DeflateStream to do the compression, not to mention having to include a computation for the Adler32 checksum.
Here's a quick untested example of what I currently have to do for decompression to give you an idea of the "lame hacky-ness": (Note: I'm not checking the checksum here. I'm assuming the data is good.)
Proposed API