15270

LZW Data Compression

Question:

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.

Answer1:

There is an implementation <a href="http://sharplzw.sourceforge.net/" rel="nofollow">here</a>.

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

Answer2:

A c# implementation of LZW: <a href="http://code.google.com/p/sharp-lzw/" rel="nofollow">http://code.google.com/p/sharp-lzw/</a>

Answer3:

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(); } } }

Answer4:

For anyone stumbling on this... I found an exact C# implementation of <a href="https://marknelson.us/posts/1989/10/01/lzw-data-compression.html" rel="nofollow">the algorithm as described in the article on Mark Nelson's website</a> on github, here:

<a href="https://github.com/pevillarreal/LzwCompressor" rel="nofollow">https://github.com/pevillarreal/LzwCompressor</a>

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.

Recommend

  • R, plot , font size changes in multiple plot figures
  • CSS3 Scale combined with reflect makes blurred elements on chrome
  • pseudocolors in R
  • how to close a show() window but keep the figure alive?
  • DLNA/UPNP Technology with iOS 10
  • Convert Bitmap to WebP Image?
  • how do i write assembly code from c#?
  • Memory error in python- how to use more memory
  • Differences in dis-assembled C code of GCC and Borland?
  • Set the selected item in dropdownlist in MVC3
  • Algorithm for a smudge tool?
  • ilmerge with a PFX file
  • Why value captured by reference in lambda is broken? [duplicate]
  • Spring Data JPA custom method causing PropertyReferenceException
  • Can I display google adwords (AdView) in javafx on android
  • Possible to stop flickering java tooltip in heavyweight mode?
  • output of program is not same as passed argument
  • Validaiting emails with Net.Mail MailAddress
  • sending/ receiving email in Java
  • Akka Routing: Reply's send to router ends up as dead letters
  • AT Commands to Send SMS not working in Windows 8.1
  • bootstrap to use multiple ng-app
  • Cannot Parse HTML Data Using Android / JSOUP
  • How to get icons for entities from eclipse?
  • How to delete a row from a dynamic generate table using jquery?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper way to use connect-multiparty with express.js?
  • JTable with a ScrollPane misbehaving
  • Java static initializers and reflection
  • Turn off referential integrity in Derby? is it possible?
  • apache spark aggregate function using min value
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • JaxB to read class hierarchy
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • Observable and ngFor in Angular 2
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?