qlabfgerkaSmurf
qlabfgerkaSmurf

Reputation: 406

C# LZW Compression and Decompression

I have looked at the SharpZipLib library, but i do not know how i would use the LZW in that library. For example i have a List<int> of bits, which i want to compress.

Lets say List<int> bits contains the following elements {1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,1,0,1,1,1,0,0,0,0,0,0,0,1,0,0,0,1,0,1,0,0,0,0,0,0,1,1,0,1,1,1}

How could i compress this using LZW SharpZipLib or any other LZW library.

I have tried looking at the documentation and ICSharpCode.SharpZipLib.Lzw, but i haven't found anything that would help me compress the List<int> above.

Upvotes: 0

Views: 1302

Answers (1)

CodeCaster
CodeCaster

Reputation: 151720

SharpZibLib only allows decompressing LZW from a particular format, namely the Z file format.

See the Stack Overflow question LZW Data Compression for code to compress data using LZW. As for your data, I'd suggest packing those bits into bytes, create a byte array from a BitArray, then a MemoryStream over that byte array.

So something like this:

var bitArray = new BitArray(bits.Select(b => b == 1).ToArray());
var byteArray = new byte[bits.Length / 8];
bitArray.CopyTo(byteArray, 0);
// do the compression magic on byteArray, or a new MemoryStream(byteArray)

Upvotes: 1

Related Questions