</pre>
=== lzw.cpp Source Code for LZW File Compression===[[Media:lzw.cpp]]<pre>
#include <time.h>
#include "lzw.h"
/******************************************************************** ** ** This program gets a file name from the command line. It compresses the ** file, placing its output in a file named test.lzw. It then expands ** test.lzw into test.out. Test.out should then be an exact duplicate of ** the input file. ** *************************************************************************/ main(int argc, char *argv[]) { clock_t timer; CLZWCompressFile lzw; /* ** Get the file name, open it up, and open up the lzw output file[Media:LZW. */ if (argc==1) { printf("Input file name to compress?\n"); return 0; } printf("testing %s...\n", argv[1zip]); /* ** Compress the file. */ timer = clock(); int crunch = lzw.Compress(argv[1], "test.lzw"); timer = clock() - timer; //CLOCKS_PER_SEC printf("compress time=%d ms, encoding=%d, size=%u", timer, lzw.get_bits(), crunch); int filesize = lzw.u_io; printf(" (ratio=%d%%)\n", filesize ? (filesize-crunch)*100/filesize : 0); if(lzw.AnyIOErrors()) printf("***I/O ERROR***\n"); /* ** Expand the file. */ timer = clock(); int orig = lzw.Expand("test.lzw", "test.out"); timer = clock() - timer; //CLOCKS_PER_SEC printf("expand time=%d ms, encoding=%d\n", timer, lzw.get_bits()); if(lzw.AnyIOErrors()) printf("***I/O ERROR***\n"); ATLASSERT(filesize == orig); // did we mangle the file? return 0; }</pre>
=== lzw.h ===