If you need to programmatically compress text files then use the functionalities provided by
(see the documentation
] for code samples). On the other hand, if you want to code yourself the algorithm then reading a book on data compression (for instance, Salomon's: Data Compression the Complete Reference
]) is highly recommended.