2013-11-15 45 views
0

你如何編寫自定義的通貨緊縮字典和實現它你如何編寫自定義的通貨緊縮字典和執行做到這一點

{"playerX":"64","playerY":"224","playerTotalHealth":"100","playerCurrentHealth":"100","playerTotalMana":"50","playerCurrentMana":"50","playerExp":"0","playerExpTNL":"20","playerLevel":"1","points":"0","strength":"1","dexterity":"1","constitution":"1","intelligence":"1","wisdom":"1","items":["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24"],"currentMapX":"0","currentMapY":"0","playerBody":"1","playerHair":"6","playerClothes":"7"} 

這是我試圖壓縮字符串。 東西,永遠不會改變的每個變量的名稱,所以我想補充一點,到字典(這是一個JSON對象)

有很多的事情,我可以放到一個字典,如

「PlayerX的」:

「playerY」:

我試圖壓縮這小我能得到它。

我只是不知道如何將它實現成字典。我知道我必須使用一個字節[],但是如何分隔字節[]中的單詞?

目前我在下面提供的代碼將它壓縮到494的長度253。我想盡量讓它儘可能小。既然它是一個小字符串,我寧願有更多的壓縮比速度。

你沒有解決這個問題對我來說,但也許提供我能做些什麼使這串巨型小

public static void main(String[] args) 
{ 
    deflater("String"); 
} 


public static String deflater(String str) 
{ 
    System.out.println("Original: " + str + ":End"); 
    System.out.println("Length: " + str.length()); 
    byte[] input = str.getBytes(); 
    Deflater d = new Deflater(); 
    d.setInput(input); 
    d.setLevel(1); 
    d.finish(); 

    ByteArrayOutputStream dbos = new ByteArrayOutputStream(input.length); 
    byte[] buffer = new byte[1024]; 
    while(d.finished() == false) 
    { 
     int bytesCompressed = d.deflate(buffer); 
     System.out.println("Total Bytes: " + bytesCompressed); 
     dbos.write(buffer, 0, bytesCompressed); 
    } 
    try 
    { 
     dbos.close(); 
    } 
    catch(IOException e1) 
    { 
     e1.printStackTrace(); 
     System.exit(0); 
    } 
    //Dictionary implementation required! 
    byte[] compressedArray = dbos.toByteArray(); 
    String compStr = new String(compressedArray); 
    System.out.println("Compressed: " + compStr + ":End"); 
    System.out.println("Length: " + compStr.length()); 
    return null; 
} 

回答

0

字典線索和來源等僅僅是串接您共同字符串作序的字節長度小於或等於32K。你不需要分開單詞。字典沒有結構。它被簡單地用作數據來源來匹配當前字符串。您應該在字典末尾放置更常見的字符串,因爲它需要較少的位來編碼較短的距離。

+0

正是我需要知道的,謝謝! – Loligans

相關問題