Question

I'm currently trying to decompress some bytes from .mat file compressed by Zlib. I read paragraph 3, page 1-12 from MAT-File Format, it told that the bytes in .mat file is compressed by Zlib.

So, my question is, does C# have some APIs or libraries that can decompress the bytes. If I got a group of bytes like

6236 20e6 0062 1606 0860 85f2 1981 980b
4cb3 30e4 25e6 a60a 40f9 2545 9989 39f1

Then I load them into an SByte array, how could I decompress them?

I also checked some libraries online. It seems that what they can do is to decompress the whole file, instead of some bytes....

I am quite new about this respect. Any help is appreciated.

Was it helpful?

Solution

You can use InflaterInputStream from SharpZipLib. .mat file has header with 0x88 length, so you have to skip it. I took sample file from here SampleMatlabMatDataFiles.zip.

byte[] data = File.ReadAllBytes("c.mat").Skip(0x88).ToArray();
byte[] decompressedData = new byte[10000];
int decompressedLength = 0;
using (MemoryStream memory = new MemoryStream(data))
using (InflaterInputStream inflater = new InflaterInputStream(memory))
    decompressedLength = inflater.Read(decompressedData, 0, decompressedData.Length);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top