views:

71

answers:

3

I need to learn how to do Lempel–Ziv–Welch compression using pen and paper for my algorithms and data structures class. Unfortunately we have only a couple of examples in our book of how it is done. I'd like to practice compressing and decompressing text using it, but I need to find a way to check if I'm right doing it right or wrong. One of the options is make a program which will compress/decompress a string and show text. I know that it wouldn't be too difficult to make a program from zero and that it would be good learning experience, but I'd rather avoid making mistakes in implementation of algorithm.

So I'm looking for some preferably free/open source library which can compress and decompress LZW for C/C++/C#/Java.

+2  A: 

Maybe zlib fits the bill?

Mike DeSimone
I'll take a look.
AndrejaKo
zlib only provides DEFLATE (LZ77), not LZW.
Piet Delport
zlib is better for your application provided that LZW is not part of the requirements, that other compression methods can be used.
rwong
@Piet: Ah, I wasn't aware that LZ and LZW were different. I've heard them used interchangeably before. Weren't there patent issues involved with LZW at some point?
Mike DeSimone
Mike: "LZ" doesn't refer to a specific algorithm, but to the whole family of Lempel-Ziv-derived algorithms. LZ77 / LZ78 were the original publications, but there are at least a dozen variants in total.
Piet Delport
A: 

Or maybe XZ Utils:

XZ Utils

XZ Utils is free general-purpose data compression software with high compression ratio. XZ Utils were written for POSIX-like systems (GNU/Linux, *BSDs, etc.), but also work on some not-so-POSIX systems like Windows. XZ Utils are the successor to LZMA Utils.

The core of the XZ Utils compression code is based on LZMA SDK, but it has been modified quite a lot to be suitable for XZ Utils. The primary compression algorithm is currently LZMA2, which is used inside the .xz container format. With typical files, XZ Utils create 30 % smaller output than gzip and 15 % smaller output than bzip2.

XZ Utils consist of several components:

  • liblzma is a compression library with API similar to that of zlib.
  • xz is a command line tool with syntax similar to that of gzip.
  • xzdec is a decompression-only tool smaller than the full-featured xz tool.
  • A set of shell scripts (xzgrep, xzdiff, etc.) have been adapted from gzip to ease viewing, grepping, and comparing compressed files.
  • Emulation of command line tools of LZMA Utils eases transition from LZMA Utils to XZ Utils. While liblzma has a zlib-like API, liblzma doesn't include any file I/O functions. A separate I/O library is planned, which would abstract handling of .gz, .bz2, and .xz files with an easy to use API.with an easy to use API.
Dirk Eddelbuettel
The asker is looking for the **LZW** algorithm.
Piet Delport
And LZMA is an improvement successor to LZW.
Dirk Eddelbuettel
@Dirk Eddelbuettel But I don't need LZMA, I need LZW as I explained in my question.
AndrejaKo
+3  A: 

LibTIFF provides a LZW Compression Kit, and Mark Nelson's LZW Data Compression tutorial includes some working source.

You might also be interested in this article: Bringing back LZW compression

Piet Delport
the LZW compression kit link was no longer working.
rwong
Hmm, seems the link on their home page is out of date. I searched a bit and found a working link, thanks.
Piet Delport