如何撰写自定义通缩词典并实施
{"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”:
我正在尝试将其压缩到尽可能小的范围。
我只是不知道如何将它实现到字典中。我知道我必须使用byte [],但是如何在byte []中分隔单词?
目前我在下面提供的代码将它从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 :(得分:0)
字典只是连接的公共字符串,以使字节序列的长度小于或等于32K。你不需要分开单词。字典没有结构。它只是用作匹配当前字符串的数据源。您应该将更常见的字符串放在字典的末尾,因为它需要更少的位来编码更短的距离。