diff --git a/.DS_Store b/.DS_Store index 5008ddf..331c402 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..1cd9d95 --- /dev/null +++ b/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..ff659d9 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + LZWDecompression + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/LZW.class b/LZW.class deleted file mode 100644 index e0d0e3c..0000000 Binary files a/LZW.class and /dev/null differ diff --git a/TestFile b/TestFile new file mode 100644 index 0000000..f1129d3 --- /dev/null +++ b/TestFile @@ -0,0 +1 @@ +c*$#& \ No newline at end of file diff --git a/TestFile2 b/TestFile2 new file mode 100644 index 0000000..6bb3d84 --- /dev/null +++ b/TestFile2 @@ -0,0 +1 @@ +abcabcabcabc \ No newline at end of file diff --git a/bin/.gitignore b/bin/.gitignore new file mode 100644 index 0000000..9272bbb --- /dev/null +++ b/bin/.gitignore @@ -0,0 +1,4 @@ +/LZW.class +/LZWDecoder.class +/TestFile +/Tester.class diff --git a/bin/LZW.class b/bin/LZW.class new file mode 100644 index 0000000..bfe89d1 Binary files /dev/null and b/bin/LZW.class differ diff --git a/hullo b/hullo new file mode 100644 index 0000000..0818d39 Binary files /dev/null and b/hullo differ diff --git a/output 2 2.bin b/output 2 2.bin new file mode 100644 index 0000000..0818d39 Binary files /dev/null and b/output 2 2.bin differ diff --git a/output 2.bin b/output 2.bin new file mode 100644 index 0000000..0818d39 Binary files /dev/null and b/output 2.bin differ diff --git a/output 2.bin.cpgz b/output 2.bin.cpgz new file mode 100644 index 0000000..e525286 Binary files /dev/null and b/output 2.bin.cpgz differ diff --git a/output.bin b/output.bin index 8fdb8fa..0818d39 100644 Binary files a/output.bin and b/output.bin differ diff --git a/output.bin 2.cpgz b/output.bin 2.cpgz new file mode 100644 index 0000000..340b5be Binary files /dev/null and b/output.bin 2.cpgz differ diff --git a/output.bin 3.cpgz b/output.bin 3.cpgz new file mode 100644 index 0000000..a0cf4e0 Binary files /dev/null and b/output.bin 3.cpgz differ diff --git a/output.bin.cpgz b/output.bin.cpgz new file mode 100644 index 0000000..3e1865c Binary files /dev/null and b/output.bin.cpgz differ diff --git a/package.bluej b/package.bluej deleted file mode 100644 index 5bae910..0000000 --- a/package.bluej +++ /dev/null @@ -1,32 +0,0 @@ -#BlueJ package file -editor.fx.0.height=728 -editor.fx.0.width=800 -editor.fx.0.x=320 -editor.fx.0.y=57 -objectbench.height=100 -objectbench.width=776 -package.divider.horizontal=0.6 -package.divider.vertical=0.8003731343283582 -package.editor.height=422 -package.editor.width=683 -package.editor.x=212 -package.editor.y=45 -package.frame.height=600 -package.frame.width=800 -package.numDependencies=0 -package.numTargets=1 -package.showExtends=true -package.showUses=true -project.charset=UTF-8 -readme.height=58 -readme.name=@README -readme.width=47 -readme.x=10 -readme.y=10 -target1.height=50 -target1.name=LZW -target1.showInterface=false -target1.type=ClassTarget -target1.width=80 -target1.x=70 -target1.y=10 diff --git a/LZW.java b/src/LZW.java similarity index 93% rename from LZW.java rename to src/LZW.java index bd39a39..903da62 100644 --- a/LZW.java +++ b/src/LZW.java @@ -1,6 +1,9 @@ import java.io.*; +//little change; + import java.util.*; + public class LZW { private static final int BIT_0 = 1; @@ -36,6 +39,8 @@ public LZW(int BinaryLength, String newFileName) newFilename = newFileName; map = new HashMap((int)Math.pow(2, binaryLength)); binaryLength = BinaryLength; + + //filling first 255 elements in the dictionary. for(int i = 0; i < 256; i++) { String binString = intToBinary(i); @@ -45,7 +50,7 @@ public LZW(int BinaryLength, String newFileName) } } - public void encode(String filepath) throws IOException + public String encode(String filepath) throws IOException { int counter = 256; @@ -88,6 +93,7 @@ else if(nextInt == -1) } writeToBinary(sb.toString()); + return (sb.toString()); } @@ -109,6 +115,7 @@ public static void writeToBinary(String s) throws IOException FileOutputStream fos = new FileOutputStream(newFilename); fos.write(l_raw); + fos.close(); } public static String intToBinary(int num) @@ -122,10 +129,5 @@ public static String intToBinary(int num) return binString; } - public static void main(String[] args) throws IOException { - // TODO Auto-generated method stub - LZW lzw = new LZW(9, "output.bin"); - lzw.encode("lzw-file3.txt"); - } } \ No newline at end of file diff --git a/src/LZWDecoder.java b/src/LZWDecoder.java new file mode 100644 index 0000000..e8e25ad --- /dev/null +++ b/src/LZWDecoder.java @@ -0,0 +1,137 @@ +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.PrintWriter; +import java.util.HashMap; + +public class LZWDecoder { + + private HashMap dict; + private int bitNum; + private String binString; + private byte [] byteArray; + private String finalOutput; + + public LZWDecoder(String binString, int bitNum, String outputFileName) throws IOException + { + this.dict = new HashMap (); + this.bitNum = bitNum; + this.binString = binString; + this.finalOutput = ""; + + +// File binFile = new File (binFileName); +// readBinFile(binFile); + decode(); + writeToTxt(outputFileName); + + } + + //reads the binary file into a string. not working, so I've bypassed this method for now and just input a binary string into the constructor. + public void readBinFile(File binFile) throws IOException + { + FileInputStream is = new FileInputStream(binFile); + + int currInt = is.read(); + while (currInt != -1) + { + binString+=currInt; + } + is.close(); + } + + + + public void decode () + { + for (int x = 0; x<256; x++) + { + char ch = (char)x; + dict.put(x, String.valueOf(ch)); + } + String binStringCopy = binString; + String currBinString = binString.substring (0,bitNum); + binString= binString.substring(bitNum); + int currDecimal = Integer.parseInt(currBinString, 2); + String currString = dict.get(currDecimal); +// finalOutput = currString; + + String nextBinString = ""; + int nextDecimal= 0; + String nextString= ""; + +// String lastSymbolInDict = ""; + + nextBinString = binString.substring(0, bitNum); + binString= binString.substring(bitNum); + nextDecimal = Integer.parseInt(nextBinString, 2); + nextString = dict.get(nextDecimal); + + int counter = 256; + while (binString.length()>= bitNum) + { + + if (nextString!= null) + { + dict.put(counter, currString+ nextString.substring(0,1)); + currString = nextString; + + + + } + else + { + dict.put(counter, currString + currString.substring(0,1)); + currString = currString + currString.substring(0,1); + } + + nextBinString = binString.substring(0, bitNum); + binString= binString.substring(bitNum); + nextDecimal = Integer.parseInt(nextBinString, 2); + nextString = dict.get(nextDecimal); + + counter++; + } + + for (int x = 0; x= 0; i--) { +// if (num.charAt(i) == '1') +// dec_value += base; +// base = base * 2; +// } +// +// return dec_value; +// } + +} + diff --git a/src/Tester.java b/src/Tester.java new file mode 100644 index 0000000..88d5c3d --- /dev/null +++ b/src/Tester.java @@ -0,0 +1,13 @@ +import java.io.IOException; + +public class Tester { + public static void main(String[] args) throws IOException { + LZW compressor = new LZW(9, "TestFile"); + + String testString = compressor.encode("lzw-file1.txt"); + System.out.println (testString); + + LZWDecoder expander = new LZWDecoder (testString, 9, "TestFile2"); + + } +}