2011-04-13 14 views
5

निम्न कोड 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 कोशिश, मुझे कोई त्रुटि मिलती है लेकिन परिणाम के लिए शून्य बाइट्स:

यहाँ प्रश्न में कोड है।

क्या डिफ्लेटर/इन्फ्लेटर के साथ कस्टम डिक्शनरी का उपयोग करने के लिए मुझे कुछ खास करने की ज़रूरत है?

उत्तर

7

मैंने वास्तव में सवाल तैयार करते समय यह पता लगाया लेकिन सोचा कि मुझे प्रश्न भी पोस्ट करना चाहिए ताकि दूसरों को मेरे संघर्षों से फायदा हो सके।

यह पता चला है कि आपको इनपुट सेट करने के बाद एक बार inflate() को कॉल करना होगा लेकिन शब्दकोश को सेट करने से पहले। वापस दिया गया मान 0 होगा, और जरूरतों के लिए एक कॉल() फिर सत्य वापस आ जाएगा। इसके बाद आप शब्दकोश सेट कर सकते हैं और दोबारा कॉल कर सकते हैं।

संशोधन कोड इस प्रकार है:

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); 
     byte[] result = new byte[100]; 
     decompresser.inflate(result); 
     decompresser.setDictionary(dict); 
     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); 
    } 
} 

यह बहुत काउंटर सहज और एक API डिजाइन के नजरिए से भद्दा लगता है, इसलिए मुझे प्रबुद्ध कृपया अगर कोई बेहतर विकल्प हैं।

+4

'needDictionary()' का कारण यह है कि zlib प्रारूप एक ही अनुप्रयोग में विभिन्न शब्दकोशों के उपयोग की अनुमति देता है, और फ़ाइल हेडर में शब्दकोश के एडलर 32-चेकसम में इंगित करता है। इस हेडर को पढ़ने के लिए (और सही डिक्शनरी चुनने के लिए डिकंप्रेसिंग पक्ष पर एप्लिकेशन को अनुमति दें), 'inflate' की पहली कॉल की आवश्यकता है। –

+0

जब मैंने ऐसा किया, तो मुझे फुलाए जाने से पहले शब्दकोश सेट करने की आवश्यकता थी() जो जानबूझकर समझ में आता है। तो आपके उदाहरण में कुछ ख़राब है ... – Chris

संबंधित मुद्दे