2.27. Compressing Strings

The Zlib library provides a way of compressing and decompressing strings and files.

Why might we want to compress strings in this way? Possibly to make database I/O faster, to optimize network usage, or even to obscure stored strings so that they are not easily read.

The Deflate and Inflate classes have class methods named deflate and inflate, respectively. The deflate method (which obviously compresses) has an extra parameter to specify the style of compression. The styles show a typical trade-off between compression quality and speed; BEST_COMPRESSION results in a smaller compressed string, but compression is relatively slow; BEST_SPEED compresses faster but does not compress as much. The default (DEFAULT_COMPRESSION ...

Get The Ruby Way: Solutions and Techniques in Ruby Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.