Click here to Skip to main content
13,089,610 members (55,187 online)
Click here to Skip to main content
Articles » Languages » Java » General » Downloads

Stats

116.5K views
4.9K downloads
10 bookmarked
Posted 13 Aug 2006

LZW Compression Algorithm Implemented in Java

, 13 Aug 2006
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)

Share

About the Author

fahadkhowaja
Web Developer
Singapore Singapore
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170813.1 | Last Updated 13 Aug 2006
Article Copyright 2006 by fahadkhowaja
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid