java如何编写自定义通缩字典并实现它
如何编写自定义通缩词典并实现它
{"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”:
“游戏性”:
我试着把它压缩到我能得到的最小值
我只是不知道如何把它编入字典。我知道我必须使用字节[],但如何在字节[]中分隔单词
目前,我在下面提供的代码将其从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;
}
# 1 楼答案
字典只是将常用字符串连接起来,形成长度小于或等于32K的字节序列。你不需要把单词分开。这本词典没有结构。它只是用作将当前字符串与匹配的数据源。你应该把更常见的字符串放在字典的末尾,因为编码更短的距离需要更少的位