如何在 Erlang 中使用 zlib 压缩列表并将其解压回来?

发布于 2024-12-24 18:24:49 字数 393 浏览 1 评论 0原文

假设我想压缩以下列表并将压缩版本保留在 RAM 中:

List = lists:seq(1,100000).

官方 文档 对我不起作用 - 我收到未绑定变量 Read 的错误,并且我不明白它的用途(是函数还是变量?) 。

我尝试在网络上搜索,但我发现的唯一内容与解压缩有关

所以,问题是:如何在 Erlang 中使用 zlib 压缩列表 List 并将其解压回来?如何查看 List 及其压缩对应项消耗了多少内存?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无远思近则忧 2024-12-31 18:24:49

term_to_binary/2 BIF 支持 zlib 压缩:

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 将识别zlib 标头并做正确的事情。

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.

雅心素梦 2024-12-31 18:24:49

您可以通过以下方式压缩数据:

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

要解压缩数据,您可以执行以下操作:

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

您可以通过检查 CData 来计算出大小。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文