0

How do you write a custom deflation dictionary and implement it

{"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"}

This is the String I am trying to compress. Things that will never change are the names of each variable, so I wanna add that to a dictionary (this is a json object)

There are a lot of things I could put into a dictionary, such as

"playerX":

"playerY":

I am trying to compress this to as small as I can get it.

I just dont know how to implement it into a dictionary. I know that I have to use a byte[], but how do I separate words in a byte[]?

Currently the code I provided below compresses it to a length of 253 from 494. I wanna try to get it as small as I can. Since it is a small String I would rather have more compression than speed.

you dont have to solve it for me, but maybe provide hints and sources etc on what I can do to make this string mega small

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;
}
Loligans
  • 497
  • 9
  • 24

1 Answers1

0

The dictionary is simply your common strings concatenated to make a sequence of bytes less than or equal to 32K in length. You do not need to separate words. There is no structure to the dictionary. It is simply used as a source of data to match the current string to. You should put the more common strings at the end of the dictionary, since it takes fewer bits to encode shorter distances back.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158