我在这里处理文本文件。我的LZW减压似乎无法正常工作。当我尝试将压缩文件解压缩时出了点问题:解压缩后,我在文本文件中添加了多余的字节。到目前为止,我发现了导致问题的值序列,它是*(星号)
例如,当我有一个包含以下句子的文本文件时:
*** Hello ***
这是我的减压方式(我仅显示2次迭代):
#1 iteration
priorword = 42
currword = 256
Added in dict: 256: **
Wrote in file: **
#2 iteration
priorword = 256
currword = 32
Added in dict: 257: **_ (_ means space)
Write in file: **
We know have in file: ****_
这里可能出问题了,因为您看到原始文件有3个星,所以添加了一个额外的星。
解压缩后,I get: **** Hello **
左边加星号*,这怎么可能?
解压缩方法:
public void LZW_Decompress(String input) throws IOException {
// DictSize builds up to 4k, Array_Char holds these values
Array_char = new String[4096];
for (int i = 0; i < 256; i++) {
dictionary.put(i, Character.toString((char) i));
Array_char[i] = Character.toString((char) i);
}
// Read input as uncompressed file & Write out compressed file
RandomAccessFile in = new RandomAccessFile(input, "r");
RandomAccessFile out = new RandomAccessFile(input.replace(
".lzw", ""), "rw");
try {
// Gets the first word in code and outputs its corresponding char
buffer[0] = in.readByte();
buffer[1] = in.readByte();
priorword = getvalue(buffer[0], buffer[1], onleft);
onleft = !onleft;
out.writeBytes(Array_char[priorword]);
// Reads every 3 bytes and generates corresponding characters
while (true) {
if (onleft) {
buffer[0] = in.readByte();
buffer[1] = in.readByte();
currword = getvalue(buffer[0], buffer[1], onleft);
} else {
buffer[2] = in.readByte();
currword = getvalue(buffer[1], buffer[2], onleft);
}
onleft = !onleft;
if (currword >= dictSize) {
if (dictSize < 4096) {
Array_char[dictSize] = Array_char[priorword]
+ Array_char[priorword].charAt(0);
}
dictSize++;
out.writeBytes(Array_char[priorword]
+ Array_char[priorword].charAt(0));
} else {
if (dictSize < 4096) {
Array_char[dictSize] = Array_char[priorword]
+ Array_char[currword].charAt(0);
}
dictSize++;
out.writeBytes(Array_char[currword]);
}
priorword = currword;
}
} catch (EOFException e) {
in.close();
out.close();
}
}