-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLzipping.java
More file actions
246 lines (218 loc) · 5.24 KB
/
Copy pathLzipping.java
File metadata and controls
246 lines (218 loc) · 5.24 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package prog;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class Lzipping {
public static int btsz;
public static String big;
public static int btoi(Byte bt) {
int ret = bt;
if (ret < 0) {
ret += 256;
}
return ret;
}
/*
* =========================================================================
* = make the integer to bin then return btsz size's string
* =========================================================================
*/
public static String fil(int inp) {
String ret = "", r1 = "";
if (inp == 0)
ret = "0";
int i;
while (inp != 0) {
if ((inp % 2) == 1)
ret += "1";
else
ret += "0";
inp /= 2;
}
for (i = ret.length() - 1; i >= 0; i--) {
r1 += ret.charAt(i);
}
while (r1.length() != btsz) {
r1 = "0" + r1;
}
return r1;
}
/*
* =========================================================================
* =
*/
/*
* =========================================================================
* = string to byte
* =========================================================================
* =
*/
public static Byte strtobt(String in) {
int i, n = in.length();
byte ret = 0;
for (i = 0; i < n; i++) {
ret *= 2.;
if (in.charAt(i) == '1')
ret++;
}
for (; n < 8; n++)
ret *= 2.;
Byte r = ret;
return r;
}
/*
* =======================================================================
*/
/*
* =========================================================================
* = precalcs the length of the
* =========================================================================
*/
public static void precalc(String fileis) {
Map<String, Integer> dictionary = new HashMap<String, Integer>();
int dictSize = 256;
for (int i = 0; i < 256; i++)
dictionary.put("" + (char) i, i);
int mpsz = 256;
String w = "";
File filei = null;
filei = new File(fileis);
try {
FileInputStream file_input = new FileInputStream(filei);
DataInputStream data_in = new DataInputStream(file_input);
Byte c;
int ch;
while (true) {
try {
c = data_in.readByte();
ch = btoi(c);
String wc = w + (char) ch;
if (dictionary.containsKey(wc))
w = wc;
else {
if (mpsz < 100000) {
dictionary.put(wc, dictSize++);
mpsz += wc.length();
}
w = "" + (char) ch;
}
} catch (EOFException eof) {
System.out.println("End of File");
break;
}
}
file_input.close();
data_in.close();
} catch (IOException e) {
System.out.println("IO exception = " + e);
}
// if empty file
if (dictSize <= 1) {
btsz = 1;
} else {
btsz = 0;
long i = 1;
while (i < dictSize) {
i *= 2;
btsz++;
}
}
filei = null;
}
/*
* =========================================================================
* ==
*/
/*
* =========================================================================
* === zipping function
* =========================================================================
* ====
*/
public static void Lamzip(String fileis) {
Map<String, Integer> dictionary = new HashMap<String, Integer>();
int dictSize = 256;
big = "";
for (int i = 0; i < 256; i++)
dictionary.put("" + (char) i, i);
int mpsz = 256;
String w = "";
String fileos = fileis + ".LmZWp";
File filei, fileo;
filei = new File(fileis);
fileo = new File(fileos);
try {
FileInputStream file_input = new FileInputStream(filei);
DataInputStream data_in = new DataInputStream(file_input);
FileOutputStream file_output = new FileOutputStream(fileo);
DataOutputStream data_out = new DataOutputStream(file_output);
data_out.writeInt(btsz);
Byte c;
int ch;
while (true) {
try {
c = data_in.readByte();
ch = btoi(c);
String wc = w + (char) ch;
if (dictionary.containsKey(wc))
w = wc;
else {
big += fil(dictionary.get(w));
while (big.length() >= 8) {
data_out.write(strtobt(big.substring(0, 8)));
big = big.substring(8, big.length());
}
if (mpsz < 100000) {
dictionary.put(wc, dictSize++);
mpsz += wc.length();
}
w = "" + (char) ch;
}
} catch (EOFException eof) {
System.out.println("End of File");
break;
}
}
if (!w.equals("")) {
big += fil(dictionary.get(w));
while (big.length() >= 8) {
data_out.write(strtobt(big.substring(0, 8)));
big = big.substring(8, big.length());
}
if (big.length() >= 1) {
data_out.write(strtobt(big));
}
}
data_in.close();
data_out.close();
file_input.close();
file_output.close();
} catch (IOException e) {
System.out.println("IO exception = " + e);
}
filei = null;
fileo = null;
}
/*
* =========================================================================
* ====
*/
public static void beginLzipping(String arg1) {
btsz = 0;
big = "";
precalc(arg1);
Lamzip(arg1);
btsz = 0;
big = "";
}
/*
* public static void main(String[] args) { btsz=0; big="";
* precalc("in.txt"); Lamzip("in.txt"); btsz=0; big=""; }
*/
}