我正在尋找可以壓縮和解壓縮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