Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » Java » General » Downloads
 
Add your own
alternative version

LZW Compression Algorithm Implemented in Java

, 13 Aug 2006 CPOL
This article provides an implementation of the LZW compression algorithm in Java
lzw_src.zip
src
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)

Share

About the Author

fahadkhowaja
Web Developer
Singapore Singapore
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 13 Aug 2006
Article Copyright 2006 by fahadkhowaja
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid