我正在寻找可以压缩和解压缩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. GIF LZW压缩在C#
- 6. LZW压缩和字典
- 7. Golang:无法使用lzw包解压缩数据
- 8. tiff lzw压缩比原始jpeg压缩大10倍
- 9. LZW压缩方法不使用imagick压缩TIFF图像
- 10. 是否可以使lzw压缩/解压缩并行?
- 11. 在java中的lzw解压缩
- 12. PERL LZW压缩与输出码长
- 13. LZW减压用C
- 14. 解决JSend中LZW解压缩php函数的问题
- 15. 数据压缩
- 16. 压缩数据
- 17. 压缩数据
- 18. GIF LZW解压提示?
- 19. 客户端数据压缩/解压缩?
- 20. JavaScript:数据流压缩/解压缩
- 21. 数据压缩和解压缩
- 22. .NET使用压缩和非压缩数据压缩文件
- 23. 数据压缩3
- 24. 压缩数据库
- 25. 压缩PCM数据
- 26. mysql数据压缩
- 27. WebSocket数据压缩
- 28. 无法读取tiff与lzw压缩jai编码解码器api
- 29. 理解LZW解压缩算法的一个例子
- 30. LZW或JBIG是更好的图像无损压缩算法吗?
你需要使用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