import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Map.Entry;
import java.util.stream.Collectors;
public class LZW {
private static Map<String, Integer> baseDict = new HashMap<String, Integer>();
private static Map<Integer, String> revDict = new HashMap<Integer, String>();
private static Scanner in = new Scanner(System.in);
public static void makeText() {
try {
FileOutputStream fos = new FileOutputStream("text.txt");
for (int n = 0; n < 5000; n++) {
for (int i = 0; i < 256; i++) {
char ch = (char) i;
fos.write(ch);
}
}
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void deflate(String fileName) {
Map<String, Integer> dict = baseDict;
try {
FileInputStream fis = new FileInputStream(fileName);
FileOutputStream fos = new FileOutputStream("compressed.lzw");
DataInputStream dis = new DataInputStream(fis);
DataOutputStream dos = new DataOutputStream(fos);
int iBuf;
char cBuf;
String sBuf = "";
iBuf = fis.read();
cBuf = (char) iBuf;
sBuf += cBuf;
while ((iBuf = fis.read()) != -1) {
cBuf = (char) iBuf;
String temp = sBuf + cBuf;
if (dict.containsKey(temp)) {
sBuf += cBuf;
} else {
fos.write(dict.get(sBuf));
dict.put(temp, dict.size());
sBuf = "";
sBuf += cBuf;
}
}
fos.write(dict.get(sBuf));
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void inflate(String fileName) {
Map<Integer, String> dict = revDict;
try {
FileInputStream fis = new FileInputStream(fileName);
FileOutputStream fos = new FileOutputStream("decompressed.pdf");
DataInputStream dis = new DataInputStream(fis);
DataOutputStream dos = new DataOutputStream(fos);
String entry, w;
char ch;
int prev, curr;
prev = dis.read();
w = dict.get(prev);
dos.writeBytes(w);
while ((curr = dis.read()) != -1) {
if (dict.containsKey(curr)) {
entry = dict.get(curr);
dos.writeBytes(entry);
dict.put(dict.size(), (w + entry.charAt(0)));
w = entry;
} else {
entry = w + w.charAt(0);
dos.writeBytes(entry);
dict.put(dict.size(), entry);
w = entry;
}
}
dis.close();
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void init() {
for (int i = 0; i < 256; i++) {
char ascii = (char) i;
baseDict.put(("" + ascii), i);
revDict.put(i, ("" + ascii));
}
deflate("original.pdf");
inflate("compressed.lzw");
}
public static void main(String[] args) {
init();
}
}
所以我一直在努力使用LZW压缩来压缩不同的文件。我的压缩功能似乎工作正常,因为我能够将5000kb pdf压缩到大约2200kb。但是我的解压缩算法无法解压缩pdf。生成的文件仍然与压缩文件大小相同,并且pdf已损坏。我正在遵循一些伪代码:
read a character k;
output k;
w = k;
while ( read a character k )
/* k could be a character or a code. */
{
if k exists in the dictionary
entry = dictionary entry for k;
output entry;
add w + entry[0] to dictionary;
w = entry;
else
output entry = w + firstCharacterOf(w);
add entry to dictionary;
w = entry;
}
有人能引导我朝正确的方向指导为什么我的减压功能是错误的吗?