The following code is based on the example provided in javadocs for java.util.zip.Deflater . The only changes I made were to create an array of bytes called dict, and then install the dictionary for both Deflater instances and Inflater using the setDictionary (byte []) method.
The problem I see is that when I call Inflater.setDictionary () with the same array as for Deflater, I get an IllegalArgumentException.
Here is the code in question:
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");
byte[] output = new byte[100];
Deflater compresser = new Deflater();
compresser.setInput(input);
compresser.setDictionary(dict);
compresser.finish();
int compressedDataLength = compresser.deflate(output);
Inflater decompresser = new Inflater();
decompresser.setInput(output, 0, compressedDataLength);
decompresser.setDictionary(dict);
byte[] result = new byte[100];
int resultLength = decompresser.inflate(result);
decompresser.end();
String outputString = new String(result, 0, resultLength, "UTF-8");
System.out.println("Decompressed String: " + outputString);
}
}
If I try to blow off the same compressed bytes without installing a dictionary, I will not get an error, but the result will be zero.
- , , Deflater/Inflater?