-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLZWCompression.java
More file actions
132 lines (129 loc) · 4.35 KB
/
LZWCompression.java
File metadata and controls
132 lines (129 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import java.util.*;
import java.io.*;
import java.nio.file.Files;
public class LZWCompression{
private final int MAX_SIZE=9;
HashMap<String,Integer> dictionary=new HashMap<String,Integer>();
public LZWCompression(){
for(int i=0;i<255;i++) dictionary.put(""+(char)(i),i);
}
public void compressFile(String file) throws IOException{
BufferedReader reader=new BufferedReader(new FileReader(file));
File compressedFile=new File(file+" compressed");
String str="";
try{
String line=reader.readLine();
while(line!=null){
str+=line;
line=reader.readLine();
}
try(FileOutputStream os=new FileOutputStream(compressedFile)){
os.write(this.compress(str));
}
reader.close();
System.out.println("File compressed succesfully");
} catch(FileNotFoundException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
}
@SuppressWarnings("deprecation")
public byte[] compress(String str){
ArrayList<Byte> arr=new ArrayList<Byte>();
String current="";
for(int i=0;str!=null&&i<str.length();i++){
char next=str.charAt(i);
if(dictionary.containsKey(current+next)){
current+=next;
}
else{
if(dictionary.get(current)!=null){
arr.add((new Integer(dictionary.get(current)/256)).byteValue());
arr.add((new Integer(dictionary.get(current)).byteValue()));
}
dictionary.put(current+next,dictionary.size()+1);
current=""+next;
}
}
if(current!=""){
arr.add((new Integer(dictionary.get(current)/256)).byteValue());
arr.add((new Integer(dictionary.get(current)).byteValue()));
}
byte[] list=new byte[arr.size()];
for(int i=0;i<arr.size();i++){
list[i]=arr.get(i);
}
return(list);
}
public String binary(int num){
String binary=Integer.toBinaryString(num);
while(binary.length()<MAX_SIZE) binary="0"+binary;
return binary;
}
public void printHashMap(){
for(HashMap.Entry<String,Integer> entry:dictionary.entrySet()) System.out.println(entry.getKey());
}
public String decompress(String compressed){
if(compressed.equals("")){
return("");
}
String str=compressed.substring(0,MAX_SIZE);
int bin=Integer.parseInt(str,2);
String value="";
for(HashMap.Entry<String,Integer> entry:dictionary.entrySet()){
if(entry.getValue().equals(bin)){
value=entry.getKey();
}
}
return ""+value+this.decompress(compressed.substring(MAX_SIZE+1));
}
public void decompressFile(String fileName){
String str="";
try{
byte[] fileContent=Files.readAllBytes((new File(fileName)).toPath());
for(int i=0;i<fileContent.length;i+=2){
int num=fileContent[i];
int num2=fileContent[i+1];
for(HashMap.Entry<String,Integer> entry:dictionary.entrySet()){
if(entry.getValue().equals(num*256+num2)){
str+=entry.getKey();
}
}
}
System.out.println(str);
} catch(IOException e){
e.printStackTrace();
}
}
public String newDecompress(String file) throws FileNotFoundException {
//takes compressed file and turns it into ArrayList of Integers
Scanner s = new Scanner(new File(file));
ArrayList<String> list = new ArrayList<String>();
while (s.hasNext()) {
list.add(s.next());
}
s.close();
// building dictionary
int size = 256;
HashMap<Integer, String> dictionary = new HashMap<Integer, String>();
for (int i = 0; i < size; i++) {
dictionary.put(i, "" + (char) i);
}
// adding compressed values back to dictionary
String place = "" + list.remove(0);
String last = place;
for (String i : list) {
String input;
if (dictionary.containsKey(i)) {
input = dictionary.get(i);
} else {
input = last + last.charAt(0);
}
last = last + input;
dictionary.put(size++, place + input.charAt(0));
place = input;
}
return last
}
}