首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LZW数据压缩

LZW数据压缩
EN

Stack Overflow用户
提问于 2012-01-03 22:35:24
回答 4查看 18.1K关注 0票数 2

我正在寻找c#中的LZW压缩算法,可以压缩和解压缩word文档。我已经在谷歌上搜索过了,但没有给出我需要的答案。谁能帮助我有它的代码,让我了解如何真正实现LZW在我的项目中。

EN

回答 4

Stack Overflow用户

发布于 2016-01-28 16:21:06

下面是我在项目中使用的LZW的实现:

代码语言:javascript
复制
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();
        }
    }
}
票数 4
EN

Stack Overflow用户

发布于 2012-01-03 22:39:46

有一个实现here

LZW并不关心它使用的是什么类型的文件。每个文件都被视为字节的blob。

票数 3
EN

Stack Overflow用户

发布于 2012-01-03 22:42:27

一种线性零树变换的c#实现:http://code.google.com/p/sharp-lzw/

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8713850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档