Eric
Eric

Reputation: 57

LZW Data Compression

I'm looking for LZW compression algorithm in c# that can compress and decompress word documents. I've search it on google but it didn't give me the answer that i need. Can anyone help me to have the code for it and for me to understand how to really implement LZW in my project.

Upvotes: 3

Views: 22384

Answers (4)

Nyerguds
Nyerguds

Reputation: 5639

For anyone stumbling on this... I found an exact C# implementation of the algorithm as described in the article on Mark Nelson's website on github, here:

https://github.com/pevillarreal/LzwCompressor

Personally, I further adapted the code to use MemoryStream instead of FileStream because I needed to convert byte arrays, not saved files, but that change is pretty trivial.

Upvotes: 3

coder3521
coder3521

Reputation: 2646

Here is the implementation of LZW which i used in my project :

namespace LZW
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<int> compressed = Compress("string to be compressed");
            Console.WriteLine(string.Join(", ", compressed));
            string decompressed = Decompress(compressed);
            Console.WriteLine(decompressed);
        }

        public static List<int> Compress(string uncompressed)
        {
            // build the dictionary
            Dictionary<string, int> dictionary = new Dictionary<string, int>();
            for (int i = 0; i < 256; i++)
                dictionary.Add(((char)i).ToString(), i);

            string w = string.Empty;
            List<int> compressed = new List<int>();

            foreach (char c in uncompressed)
            {
                string wc = w + c;
                if (dictionary.ContainsKey(wc))
                {
                    w = wc;
                }
                else
                {
                    // write w to output
                    compressed.Add(dictionary[w]);
                    // wc is a new sequence; add it to the dictionary
                    dictionary.Add(wc, dictionary.Count);
                    w = c.ToString();
                }
            }

            // write remaining output if necessary
            if (!string.IsNullOrEmpty(w))
                compressed.Add(dictionary[w]);

            return compressed;
        }

        public static string Decompress(List<int> compressed)
        {
            // build the dictionary
            Dictionary<int, string> dictionary = new Dictionary<int, string>();
            for (int i = 0; i < 256; i++)
                dictionary.Add(i, ((char)i).ToString());

            string w = dictionary[compressed[0]];
            compressed.RemoveAt(0);
            StringBuilder decompressed = new StringBuilder(w);

            foreach (int k in compressed)
            {
                string entry = null;
                if (dictionary.ContainsKey(k))
                    entry = dictionary[k];
                else if (k == dictionary.Count)
                    entry = w + w[0];

                decompressed.Append(entry);

                // new sequence; add it to the dictionary
                dictionary.Add(dictionary.Count, w + entry[0]);

                w = entry;
            }

            return decompressed.ToString();
        }
    }
}

Upvotes: 3

voidengine
voidengine

Reputation: 2579

A c# implementation of LZW: http://code.google.com/p/sharp-lzw/

Upvotes: 2

Drew Dormann
Drew Dormann

Reputation: 63957

There is an implementation here.

LZW does not care what kind of file it is working with. Every file is treated as a blob of bytes.

Upvotes: 3

Related Questions