erlang 压缩zlib

来源:互联网 发布:手机淘宝客户端改差评 编辑:程序博客网 时间:2024/06/03 11:17

一:写在之前

今天和同事讨论偶然提到了,代码中注释掉的压缩方法,想了想好像还没有看过压缩相关的内容

一位同事提出了,之前他们项目因为压缩崩溃过一次,这更激起了自己兴趣,晚上看了会,记录下一些心得


二:简介

erlang提供了一个压缩模块 zlib

那么,什么是zlib?

wiki   连接点这里

wiki中的简介

zlib is a software library used for data compression. zlib was written by Jean-Loup Gailly and Mark Adler and is an abstraction of the DEFLATE compression algorithm used in their gzip file compression program. zlib is also a crucial component of many software platforms including Linux, Mac OS X, and iOS. It has also been used in gaming consoles such as the PlayStation 4, PlayStation 3, Wii U, Wii, Xbox One and Xbox 360.

zlib是一种通用的压缩格式,而erlang 只是提供了一个api调用 zlib的api

erlang zlib简介

The zlib module provides an API for the zlib library (http://www.zlib.org). It is used to compress and decompress data. The data format is described by RFCs 1950 to 1952.

三:使用

zlib:compress(iodata())

直接使用即可,那么压缩效果和?

做个测试

BinaryPart = list_to_binart(lists:seq(1,255)),Binary =  <<BinaryPart/binary, BinaryPart/binary,BinaryPart/binary,BinaryPart/binary,            BinaryPart/binary,BinaryPart/binary,BinaryPart/binary,BinaryPart/binary,            BinaryPart/binary>>CompressBinary = zlib:compress(Binary),{byte_size(Binary), byte_size(CBinary)}
重复数据,这是非常适合压缩的情况,

结果为{3060,304}

证明压缩确实有效。。。


四:问题

有一位同事提出,zlib:compress每次都需要打开一个端口,当有压缩有异常时端口不能正常关闭,他之前项目曾经出过问题。

so...真的是这样么。。

有一篇记录到这个问题的文章,这里不再重复。原文链接

但是。。这里也提到了,15b01 erlang已经修复了这个问题  原文链接

做了个实验。。大致就是,不断的调用zlib:compress 重复100万次,一半错误,一半正确,记录打开的端口数量

最后结果是,所有端口都正常关闭了,所以可以放心使用。


0 0
原创粉丝点击