Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
6 changes: 1 addition & 5 deletions .classpath
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-14">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
2 changes: 1 addition & 1 deletion .project
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>LZWCompression</name>
<name>LZW-Compression</name>
<comment></comment>
<projects>
</projects>
Expand Down
7 changes: 6 additions & 1 deletion ReadMe
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ uses the method fillInitialValues() to fill the HashMap dict with the pre-determ
It then calls the method compress which uses a buffered reader and a for loop to run through each char in the file.
It adds combinations of these chars with arbitrary, decimal askii values to dict. The for loop also converts these
decimals to binary before adding them to the string binaryString. binaryString is converted into a char array of 0s and 1s,
which is then converted to a byte array, which is then used by the FileOutputStream to write a binary file.
which is then converted to a byte array, which is then used by the FileOutputStream to write a binary file.

ROHAN: Ariana used 9 bits
The decoder does not work, I am unsure if it is an issue with the decoder or your encoder
It outputs something for file 1, but it is not right
It gets an error for file 3
1 change: 1 addition & 0 deletions decoded.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Æ888HHH(```d
143 changes: 143 additions & 0 deletions src/RohanDecoder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import java.util.*;

import java.io.*;
import java.nio.file.*;

public class RohanDecoder {

public HashMap<Integer, String> dictionary;
public String binary;
int binaryIndex;
ArrayList<Integer> numberValues;

public RohanDecoder() {
binary = "";
dictionary = new HashMap<Integer, String>();
this.makeDictionary();
}

public void makeDictionary() {
for(int i = 0; i<256; i++) {
dictionary.put(i, "" + (char)i);
}
}

public void ByteToBinary() throws IOException {
FileInputStream input = new FileInputStream("trashbin");

binary = "";
byte[] byteArray = input.readAllBytes();
//System.out.println(byteArray);

for(int i =0;i<byteArray.length;i++)
{
//System.out.println(byteArray[i]+" "+toBinary(byteArray[i]));

binary+= this.toBinary(byteArray[i]);
//System.out.println(binary);
}
input.close();

}

public void decode() throws IOException { //algorithm from https://www.geeksforgeeks.org/lzw-lempel-ziv-welch-compression-technique/
this.ByteToBinary();
PrintWriter outputWriter = new PrintWriter(new BufferedWriter(new FileWriter("decoded.txt")));

binaryIndex = 0;

this.makeNumbersFromBinary(); //fills the variable numberValues with the number representations of each 9 bit binary string
//System.out.println(this.numberValues);

Integer old = numberValues.get(0);
outputWriter.print(dictionary.get(old)); //outputs the value from old

int nextDictIndex = 256;
String combination = dictionary.get(old);
String firstLetter = "" + combination.charAt(0);


for(int i = 1; i< numberValues.size(); i+=1) {
Integer newest = numberValues.get(i);

if(dictionary.containsKey(newest)) {
combination = dictionary.get(newest); //if it contains newest then we set the combo to its value
}
else {
combination = dictionary.get(old); //if it doenst contain newest, then we set combo to old + firstLetter
combination = combination + firstLetter;
}

outputWriter.print(combination);

firstLetter = "" + combination.charAt(0);
dictionary.put(nextDictIndex, dictionary.get(old) + combination);
nextDictIndex+=1;
old = newest;
}

outputWriter.close();
// System.out.println(old);
// System.out.println(dictionary.get(old));


// String Binary2 = binary.substring(9,18);
// System.out.println("binary " + oldBinary);
// System.out.println("binary " + Binary2);
}


public Integer getNextNumberFromBinary() {

String nextBinary = this.binary.substring(this.binaryIndex, this.binaryIndex+9);
binaryIndex+=9;
return Integer.parseInt(nextBinary,2);
}

public void makeNumbersFromBinary() {
this.numberValues = new ArrayList<Integer>();

for(int i = 0;i <= binary.length() - 9; i+=9) {
// System.out.println("next number = " + this.getNextNumberFromBinary());
// System.out.println("list = " + this.numberValues);
//System.out.println(dictionary.get(this.getNextNumberFromBinary()));
//numberValues.add(getNextNumberFromBinary());
// if(i+9 > binary.length()) {
// break;
// }

numberValues.add(binaryToNumber(binary.substring(i,i+9)));
}
}

public static int binaryToNumber(String a) //found online
{
int ans = 0;
for(int i = 0;i<9;i++)
{
if(a.charAt(i)=='1')
{
ans+=(1<<(8-i));
}
}
return ans;
}



public String toBinary(int number)
{
String cur =Integer.toBinaryString(number);
StringBuilder ans = new StringBuilder();
while(cur.length()+ans.length()<8)
{
ans.append("0");
}
if(cur.length()>8)
{
cur = cur.substring(cur.length()-8);
}
ans.append(cur);
return ans.toString();
}
}
3 changes: 3 additions & 0 deletions src/TestLZW.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ public static void main (String [] args) throws IOException
File testFile = new File ("lzw-file1.txt");
LZWCompression compressy= new LZWCompression (testFile);

RohanDecoder decode = new RohanDecoder();
decode.decode();

}
}