views:

284

answers:

2

Hello,

I want to send compressed data between my C# to a C++ application in ZLIB format. In C++, I use the zlib_compressor/zlib_decompressor available in boost::iostreams. In C#, I am currently using the ZOutputStream available in the zlib.NET library. First of all, when I compress the same data using both libraries, the results look different:

  1. boost::iostreams::zlib_compressor:

63 61 60 60 F8 00 C4 C1 25 45 99 79 E9 23 87 04 00

  1. zlib.NET (zlib.ZOutputStream):

78 9C 63 61 60 60 F8 00 C4 C1 25 45 99 79 E9 23 87 04 00 4F 31 63 8D

(Note the 78 9C pattern that is present in zlib.NET, but not in boost).

Furthermore, when I decompress data in boost that I compressed in zlib.NET, I am not able to read from the stream suggesting something is wrong. It does work when I try to decompress data compressed in boost.

Does anybody know what is going wrong?

Thank you,

Johan

A: 

I haven't figured out why zlib.NET is not compatible with Boost iostreams, but I did find a solution: I am now using DotNetZip (http://dotnetzip.codeplex.com/) and that works fine.

Johan
+3  A: 

It's because the boost::iostreams::zlib_compressor code is producing raw deflated data, whereas the zlib.NET is adding a header and footer so that the data is in the compress format.

For evidence, consider this log of a Tcl session:

% set s {78 9C 63 61 60 60 F8 00 C4 C1 25 45 99 79 E9 23 87 04 00 4F 31 63 8D}
78 9C 63 61 60 60 F8 00 C4 C1 25 45 99 79 E9 23 87 04 00 4F 31 63 8D
% set b [binary format H* [join $s ""]];string length $b
23
% zlib decompress $b
ðStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringString
% zlib inflate $b
data error
% set s {63 61 60 60 F8 00 C4 C1 25 45 99 79 E9 23 87 04 00}
63 61 60 60 F8 00 C4 C1 25 45 99 79 E9 23 87 04 00
% set b [binary format H* [join $s ""]];string length $b
17
% zlib decompress $b
data error
% zlib inflate $b
ðStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringString

(The zlib decompress command expands data in “compress” format, the zlib inflate command expands data in “raw deflated” format.)

Donal Fellows
It's also curious that the boost library appears to go out of its way to put the non-raw formats just out of reach by default, given that the underlying library supports them just fine. Must be some kind of philosophical thing which escapes me I suppose.
Donal Fellows
Thanks, great help. It turned out that I had passed the no_header option to boost by accident... Doh!
Johan