Click here to Skip to main content
15,889,839 members
Articles / Programming Languages / Java / Java SE

LZW Compression Algorithm Implemented in Java

Rate me:
Please Sign up or sign in to vote.
3.82/5 (8 votes)
13 Aug 2006CPOL 168.1K   6.1K   10  
This article provides an implementation of the LZW compression algorithm in Java
import java.io.*;

public class lzw
{
	public static void main ( String[] args )
	{
		FileInputStream  input  = null;
		FileOutputStream output = null;
		
		if( args[0] == "" )
		{
			System.out.println( "Usage: java lzw <filename>" );
			System.exit( 1 );
		}
		
		try
		{
			input = new FileInputStream( args[0] );
		} 
		catch ( FileNotFoundException fnfe )
		{
			System.out.println( "Unable to open input file: " + args[0] );
			System.exit( 1 );
		}
		
		try
		{
			output = new FileOutputStream( "compressed.lzw" );
		} 
		catch ( FileNotFoundException fnfe )
		{
			System.out.println( "Unable to open output file compressed.lzw " );
			System.exit( 1 );
		}

		LZWCompression lzw = new LZWCompression( input, output );
		
		lzw.compress();		/* compress the file */
		
		try
		{
			input.close();
			output.close();
		}
		catch ( IOException ioe )
		{
			System.out.println( "IOException in main()." );
			System.exit(1);
		}
		
		System.out.println( "Done! Compressed file: compressed.lzw");
	}
}
	

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Singapore Singapore
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions