0

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?

Kromster
  • 7,181
  • 7
  • 63
  • 111
Petey
  • 49
  • 3
  • Did you try the code in Embarcadero documentation http://docwiki.embarcadero.com/CodeExamples/Sydney/en/ZLibCompressDecompress_(Delphi) ? First try using a standard zip file. Also, did you try to decompress the file created with C# using WinZip or Windows built-in zip handling? – fpiette Nov 21 '20 at 15:14
  • I did, it works fine if compressed and decompressed using Delphi, but not with a C# compressed file. – Petey Nov 21 '20 at 15:18
  • An the file compressed with C#, can it be decompressed using Windows build-in zip file handling (just using the explorer)? Just to validate that compression done by your C# code is standard. – fpiette Nov 21 '20 at 15:53
  • 6
    You should write an answer here to your own question, instead of putting the answer in the question. – Mark Adler Nov 21 '20 at 15:55
  • I've rolled back your edit. It is inappropriate here to edit a solution into the question. If you've found that solution, you can write it up in the form of an answer in the space provided below for that purpose. See [Can I answer my own question?](http://stackoverflow.com/help/self-answer) for more information. – Ken White Nov 22 '20 at 04:46
  • fpiette - I wasn't creating zip archives in C#, just compressed files. Mark and Ken - my apologies, have posted my update as an answer now. – Petey Nov 22 '20 at 16:08

1 Answers1

3

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.

Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54
Petey
  • 49
  • 3