以下代码基于java.util.zip.Deflater 的 javadocs 中给出的示例。我所做的唯一更改是创建一个名为 dict 的字节数组,然后使用 setDictionary(byte[]) 方法在 Deflater 和 Inflater 实例上设置字典。
我看到的问题是,当我使用与 Deflater 完全相同的数组调用 Inflater.setDictionary() 时,我得到了 IllegalArgumentException。
这是有问题的代码:
import java.util.zip.Deflater;
import java.util.zip.Inflater;
public class DeflateWithDictionary {
public static void main(String[] args) throws Exception {
String inputString = "blahblahblahblahblah??";
byte[] input = inputString.getBytes("UTF-8");
byte[] dict = "blah".getBytes("UTF-8");
// Compress the bytes
byte[] output = new byte[100];
Deflater compresser = new Deflater();
compresser.setInput(input);
compresser.setDictionary(dict);
compresser.finish();
int compressedDataLength = compresser.deflate(output);
// Decompress the bytes
Inflater decompresser = new Inflater();
decompresser.setInput(output, 0, compressedDataLength);
decompresser.setDictionary(dict); //IllegalArgumentExeption thrown here
byte[] result = new byte[100];
int resultLength = decompresser.inflate(result);
decompresser.end();
// Decode the bytes into a String
String outputString = new String(result, 0, resultLength, "UTF-8");
System.out.println("Decompressed String: " + outputString);
}
}
如果我尝试在不设置字典的情况下压缩相同的压缩字节,我不会收到错误,但返回的结果是零字节。
为了将自定义字典与 Deflater/Inflater 一起使用,我需要做些什么特别的事情吗?