CSCI 203 Project 3  Huffman encryption for file compression/decompression.

You’ll need a min heap --- I recommend using the one already defined in Java 1.5 (java.util.PriorityQueue).  You may use text files or image files for input. (Text file might be a good starting point).  Provide an application with a reasonable UI.  The user should be able to enter names for file to compress and name of compressed file, or file to uncompress and output file.  The output file from compression will likely be a binary file.

Optional: For text files, you might display text in a JTextArea. For image files you could display the image on a paintpanel of some sort.

You may use an ArrayList or your own structure to store symbols, calculate frequencies, and then build a priority queue (of frequencies).  For text files, 256 entries or so should be plenty but you’ll want something fairly large and possibly resizable to handle image files.  For encryption, you should:

·        Open input file.

·        Read all chars and count frequencies.

·        Insert char/frequency objects into priority queue.  (Use heap to maintain queue)

·        Build Huffman code.

·        Write out the code…. This involves visiting the leaf nodes and printing out the symbol and (string) path to get to the leaf.  The path is the code. You may store the code in another file.

·        Apply the encryption and save the compressed file and code file.  If input file was input.txt, you could generate two output files, inputcompress.xyz and inputcode.xyz for example. 

To “decompress” a file, first read the code file and store the symbols in an array based on their codes. If you use our old algorithm, you’d multiply by 2 and add 1 each time you see a “0” and multiply by 2 and add 2 each time you see a “1”.  So whoever has code “0” goes in position 1. And “00” would be in 3. And so on.  Fill the array. Read the compressed file and output the decompressed file as follows:

Set current pos=0 (root)

While not done reading from the file{

    Read c

    If c is 0 pos=2*pos+1 else pos=2*pos+2;

    If at a leaf {print char to output; pos=0;}

}

Here’s a snippet of code for reading binary data from a file:

            String file = application.getRealPath("/") + "test.dat";
            FileInputStream fileinputstream = new FileInputStream(file);

            int numberBytes = fileinputstream.available();
            byte bytearray[] = new byte[numberBytes];

            fileinputstream.read(bytearray);
             //process data somehow, for example, print it out
            for(int i = 0i < numberBytesi++){
                out.println(bytearray[i]);
            }

            fileinputstream.close();