Given a model, near-optimal encoding and decoding can be performed using the method of ``arithmetic coding'', described in the following tutorial:
Witten, I. H., Neal, R. M., and Cleary, J. G. (1987) ``Arithmetic coding for data compression'', Communications of the ACM, vol. 30, pp. 520-540: abstract, associated software.A newer implementation of arithmetic coding is described in the following paper:
Moffat, A., Neal, R. M., and Witten, I. H. (1998) ``Arithmetic coding revisited'', ACM Transactions on Information Systems, vol. 16, pp. 256-294: abstract, associated reference, associated software.You can also obtain software implementing arithmetic coding, including the methods described in both the above papers.