निम्न कोड javadocs for java.util.zip.Deflater में दिए गए उदाहरण पर आधारित है। मेरे द्वारा किए गए एकमात्र परिवर्तनों को एक बाइट सरणी बनाना है जिसे dict कहा जाता है और फिर setDictionary (बाइट []) विधि का उपयोग करके Deflater और Inflater उदाहरणों पर शब्दकोश को सेट करें।कस्टम डिक्शनरी के साथ जावा डिफ्लेटर/इन्फ्लेटर का उपयोग अवैध कारणों का कारण बनता है IllegalArgumentException
समस्या जो मैं देख रहा हूं वह यह है कि जब मैं Deflater.setDictionary() को उसी सरणी के साथ कॉल करता हूं जैसा कि मैंने डिफ्लेटर के लिए उपयोग किया था, मुझे एक अवैध अर्ग्यूमेंट अपवाद मिलता है।
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);
}
}
अगर मैं शब्दकोश की स्थापना के बिना एक ही संकुचित बाइट्स deflating कोशिश, मुझे कोई त्रुटि मिलती है लेकिन परिणाम के लिए शून्य बाइट्स:
यहाँ प्रश्न में कोड है।
क्या डिफ्लेटर/इन्फ्लेटर के साथ कस्टम डिक्शनरी का उपयोग करने के लिए मुझे कुछ खास करने की ज़रूरत है?
'needDictionary()' का कारण यह है कि zlib प्रारूप एक ही अनुप्रयोग में विभिन्न शब्दकोशों के उपयोग की अनुमति देता है, और फ़ाइल हेडर में शब्दकोश के एडलर 32-चेकसम में इंगित करता है। इस हेडर को पढ़ने के लिए (और सही डिक्शनरी चुनने के लिए डिकंप्रेसिंग पक्ष पर एप्लिकेशन को अनुमति दें), 'inflate' की पहली कॉल की आवश्यकता है। –
जब मैंने ऐसा किया, तो मुझे फुलाए जाने से पहले शब्दकोश सेट करने की आवश्यकता थी() जो जानबूझकर समझ में आता है। तो आपके उदाहरण में कुछ ख़राब है ... – Chris