C# compression and Delphi decompression

391 views Asked by At

I've some files that were compressed using the C# DeflateStream class like this:

using (DeflateStream compressionStream = new DeflateStream(compressedFileStream, CompressionMode.Compress))

Nothing fancy, all default values. It decompresses fine using the equivalent C# DeflateStreamCode i.e.

using (DeflateStream decompressionStream = new DeflateStream(originalFileStream, CompressionMode.Decompress))

but I need to decompress this using Delphi. I've tried the zlib library (XE8, XE10.3), something along these lines:

InStream := TMemoryStream.Create;
InStream.LoadFromFile('F:\mycompressedfile.x');
DecompressionStream := TDecompressionStream.Create(InStream);
OutStream := TMemoryStream.Create;
OutStream.LoadFromStream(DecompressionStream);
OutStream.SaveToFile('F:\mydecompressedfile.x');

but keep getting 'data error' messages. How can I decompress a C# DeflateStream compressed file?

1

There are 1 answers

0
Petey On

Apparently, the default settings for the Delphi deflate algorithms expect zlib headers, while the C# deflate code creates 'raw' compressed streams. Setting up the decompression stream this way (with a -ve value for WindowBits) allows Delphi to decompress the C# compressed data.

DecompressionStream := TZDecompressionStream.Create(FBlobStream, -15);

As per the official documentation:

The WindowBits parameter determines the buffer handling. Zero indicates the zlib header to determine the buffer size. Values between 8 and 15 set the buffer size, negative values indicate the raw processing, and adding to 16 forces the gzip handling.