我正在寻找可以压缩和解压缩Word文档的C#中的LZW压缩算法。我在谷歌搜索它,但它没有给我我需要的答案。任何人都可以帮助我获得它的代码,并让我了解如何在我的项目中真正实现LZW。LZW数据压缩
LZW数据压缩
回答
一个C#实现的LZW: http://code.google.com/p/sharp-lzw/
我无法下载,甚至看不到代码。 – Eric 2012-01-03 15:17:13
转到“源代码”,然后浏览代码或按照说明检出项目 – voidengine 2012-01-03 15:19:12
下面是我在我的项目中使用的LZW的实施:
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();
}
}
}
但是,为什么要使用列表
它不仅仅是用于字符串。 ?你试图达到什么样的目的,你可以根据你的需求修改它,你也可以修改它来使用字节数组,而不是int,这只是一个示例。切记叠加流程仅供参考,并非编码服务:) – csharpcoder 2017-07-12 03:44:56
LZW还用于非文本数据压缩。但是真的很难找到这样的例子:(至于我想要达到的目标,我正在研究在二进制文件存储中使用LZW的旧DOS游戏的修改工具。 – Nyerguds 2017-07-13 10:11:28
对于任何绊脚石这个...我发现一个确切的C#实现的the algorithm as described in the article on Mark Nelson's website在GitHub上,在这里:
https://github.com/pevillarreal/LzwCompressor
每在声音上,我进一步修改代码以使用MemoryStream
而不是FileStream
,因为我需要转换字节数组,而不是保存的文件,但这种改变非常微不足道。
- 1. 阵LZW压缩
- 2. 吉夫LZW压缩
- 3. Java LZW压缩和解压缩图像
- 4. LZW在Lua中的压缩
- 5. LZW压缩和字典
- 6. tiff lzw压缩比原始jpeg压缩大10倍
- 7. PERL LZW压缩与输出码长
- 8. 在java中的lzw解压缩
- 9. LZW减压用C
- 10. 数据压缩
- 11. 客户端数据压缩/解压缩?
- 12. 数据压缩和解压缩
- 13. mysql数据压缩
- 14. 数据压缩3
- 15. WebSocket数据压缩
- 16. 理解LZW解压缩算法的一个例子
- 17. LZW或JBIG是更好的图像无损压缩算法吗?
- 18. 无法读取tiff与lzw压缩jai编码解码器api
- 19. 在Erlang中解压压缩的数据
- 20. 透明SQLite数据压缩
- 21. 压缩小块数据
- 22. 在Firebase中压缩数据
- 23. postgresql tcp数据压缩
- 24. HTTP/2数据压缩
- 25. GWT中的数据压缩
- 26. 数据压缩Http 1.1
- 27. 压缩当前数据库
- 28. 如何解压缩字节数组中的压缩数据?
- 29. ADO.NET数据服务的数据压缩
- 30. 压缩来自数据库的数据
你需要使用LZW而不是GZip的任何理由? – 2012-01-03 14:37:44
可能[重复](http://stackoverflow.com/questions/6710014/lzw-compression-on-c-sharp-from-string) – 2012-01-03 14:42:30
我会问同样的 - 具体使用LZW的原因是什么?您是否在与使用LZW的其他系统交互时尝试进行压缩/解压缩?如果是这样,则用户发布的实现可能与您的外部系统不兼容。请澄清这个问题。祝好, – 2012-01-03 14:47:51