How to write a Java Program to Creates a class that compresses the file that is in-putted ?
Solution:
//Creates a class that compresses the file that is inputted import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Compress { //Creates a static int type to store the default ASCII values that will be appended static int defaultValues = 256; //Creates a variable that reads the current byte in the file int readByte = 0; //Creates a constant int type to store the size of the dictionary/hash table final static int MAX_TABLE_SIZE = 4096; public static void main (String[] args) throws IOException { String key = ""; //Creates a new Dictionary and initializes it with the set size Dictionary newDict = new Dictionary(MAX_TABLE_SIZE); //String stringValue = new Character((char)).toString(); int readByte; MyOutput fileOut = new MyOutput(); int appendCode = 256; String inputFile = args[0]; String outputFile = inputFile + ".zzz"; try { BufferedInputStream in; in = new BufferedInputStream(new FileInputStream(inputFile)); BufferedOutputStream out; out = new BufferedOutputStream(new FileOutputStream(outputFile)); for (int i = 0; i < defaultValues; i++) { String stringValue2 = new Character((char)i).toString(); DictEntry newEntry = new DictEntry(stringValue2, i); newDict.insert(newEntry); } readByte=in.read(); String appendString = ""; //String value = new Character((char)readByte).toString(); while (in.read()!=-1) //&& newDict.find(appendString).equals(null)) { String value = new Character((char)readByte).toString(); appendString += value; //appendString += (char)readByte; /*if (readByte == -1) { System.out.println(""); break; }*/ if (newDict.find(appendString) == null || newDict.find(appendString).equals(null)) { //appendString += value; DictEntry newString = new DictEntry(appendString, appendCode); if (newDict.numElements() < MAX_TABLE_SIZE) { newDict.insert(newString); } appendCode++; //appendString += (char)readByte;//Integer.toString(appendCode); //fileOut.output(appendCode, out); //appendString = ""; fileOut.output(appendCode, out); //outputkey appendString = ""; //System.out.print(newDict.find(appendString).getCode()); } else { key = appendString; readByte = in.read(); } appendString = value; //readByte = in.read(); } //readByte = in.read(); fileOut.output(appendCode, out);//out put key again //fileOut.flush(out); if (in != null) { in.close(); } //fileOut.flush(out); if (out != null) { out.close(); } fileOut.flush(out); } catch (DictionaryException e) { System.out.println("error"); } } }