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.
10 changes: 10 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?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="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>LZWDecompression</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Binary file removed LZW.class
Binary file not shown.
1 change: 1 addition & 0 deletions TestFile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c*$�����#&
1 change: 1 addition & 0 deletions TestFile2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
abcabcabcabc
4 changes: 4 additions & 0 deletions bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/LZW.class
/LZWDecoder.class
/TestFile
/Tester.class
Binary file added bin/LZW.class
Binary file not shown.
Binary file added hullo
Binary file not shown.
Binary file added output 2 2.bin
Binary file not shown.
Binary file added output 2.bin
Binary file not shown.
Binary file added output 2.bin.cpgz
Binary file not shown.
Binary file modified output.bin
Binary file not shown.
Binary file added output.bin 2.cpgz
Binary file not shown.
Binary file added output.bin 3.cpgz
Binary file not shown.
Binary file added output.bin.cpgz
Binary file not shown.
32 changes: 0 additions & 32 deletions package.bluej

This file was deleted.

14 changes: 8 additions & 6 deletions LZW.java → src/LZW.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import java.io.*;
//little change;

import java.util.*;


public class LZW {

private static final int BIT_0 = 1;
Expand Down Expand Up @@ -36,6 +39,8 @@ public LZW(int BinaryLength, String newFileName)
newFilename = newFileName;
map = new HashMap<String, String>((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);
Expand All @@ -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;

Expand Down Expand Up @@ -88,6 +93,7 @@ else if(nextInt == -1)
}

writeToBinary(sb.toString());
return (sb.toString());

}

Expand All @@ -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)
Expand All @@ -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");
}
}
137 changes: 137 additions & 0 deletions src/LZWDecoder.java
Original file line number Diff line number Diff line change
@@ -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 <Integer, String> 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 <Integer, String>();
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<binStringCopy.length()/bitNum;x++)
{
String currBinStringCopy = binStringCopy.substring(0,bitNum);
binStringCopy= binStringCopy.substring(bitNum);
finalOutput+=dict.get(Integer.parseInt(currBinStringCopy, 2));
}
}

//writes the
public void writeToTxt(String outputFileName) throws FileNotFoundException
{
PrintWriter output = new PrintWriter(outputFileName);
output.print (finalOutput);
output.close();
}

// class GFG {
//
// // Function to convert binary to decimal
// private static int binaryToDecimal (String n)
// {
// String num = n;
// int dec_value = 0;
//
// // Initializing base value to 1,
// // i.e 2^0
// int base = 1;
//
// int len = num.length();
// for (int i = len - 1; i >= 0; i--) {
// if (num.charAt(i) == '1')
// dec_value += base;
// base = base * 2;
// }
//
// return dec_value;
// }

}

13 changes: 13 additions & 0 deletions src/Tester.java
Original file line number Diff line number Diff line change
@@ -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");

}
}