skanatek
skanatek

Reputation: 5251

How can I compress a list with zlib in Erlang and decompress it back?

Let's say I would like to compress the following list and keep the compressed version in RAM:

List = lists:seq(1,100000).

The example provided in the official documentation doesn't work for me - I get the error for unbound variable Read and I do not understand what it is used for (is it a function or a variable?).

I have tried to search on the web, but the only thing that I have found is related to decompressing files.

So, the question is: How can I compress the list List and decompress it back with the help of zlib in Erlang? How can I see what amount of memory is consumed by the List and its compressed counterpart?

Upvotes: 3

Views: 2309

Answers (2)

uwiger
uwiger

Reputation: 666

The term_to_binary/2 BIF supports zlib compression:


Eshell V5.8.4  (abort with ^G)
1> L = lists:seq(1,100000).
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,
 23,24,25,26,27,28,29|...]
2> B1 = term_to_binary(L).
<<131,108,0,1,134,160,97,1,97,2,97,3,97,4,97,5,97,6,97,7,
  97,8,97,9,97,10,97,11,97,...>>
3> size(B1).
499242
4> B2 = term_to_binary(L,[compressed]).
<<131,80,0,7,158,41,120,156,20,212,103,27,134,1,192,8,84,
  239,189,247,222,235,81,68,73,200,46,35,84,...>>
5> size(B2).
212752

binary_to_term/1 will recognize the zlib header and do the right thing.

Upvotes: 9

Ionel Gog
Ionel Gog

Reputation: 326

You can compress the data in the following manner:

Z=zlib:open(),
zlib:deflateInit(Z),
CData=zlib:deflate(Z2, lists:seq(1,100), finish),
zlib:deflateEnd(Z).

To decompress the data you can do:

Z=zlib:open(),
zlib:inflateInit(Z),
Data=zlib:Inflate(Z, CData),
zlib:inflateEnd(Z).

You can just figure out the size by checking CData.

Upvotes: 6

Related Questions