0910 - iPaste 搞定数据压缩

在 iPaste 数据结构中,有个很重要的环节:压缩数据(即剪贴板内容);鬼使神差的,竟然一直没有做。在准备发 Beta 2 时,做吧。

简单了解下后,发现Apple 内置了 libcompression library 压缩库。而且,也有一些使用 Swift 的封装,比如 NSData-CompressionDataCompression;我使用了后者,不过应该差不多。

进一步,这个库中包含了 4 种压缩算法:

  • LZFSE
  • LZ4
  • LZMA
  • ZLIB

如何选择呢?苹果也给出了官方建议:

Choice of Compression Algorithm:

  • Use LZ4 if speed is critical, and you are willing to sacrifice compression ratio to achieve it.
  • Use LZMA if compression ratio is critical, and you are willing to sacrifice speed to achieve it. Note that - LZMA is an order of magnitude slower for both compression and decompression than other choices.
  • Otherwise, if speed and compression are more or less equally important, use LZFSE unless you require interoperability with non-Apple devices. If you do require interoperability with non-Apple devices, use ZLIB.
  • LZFSE is faster than ZLIB, and generally achieves a better compression ratio. However, it is slower than LZ4 and does not compress as well as LZMA, so you will still want to use LZ4 if speed is critical or LZMA if compression ratio is critical.

简单的说:

**- LZMA 压缩效果最好

  • LZ4 压缩速度最快
  • LZFSE 是苹果自己开发的、综合表现好
  • ZLIB 应该被 LZFSE 取代**

当然,不能道听途说,我也拿自己的实践数据测试了一把:

最后,我选择了苹果自己的 LZFSE 压缩算法。

除了压缩本身,还有件很重要的事:兼容已有的、未压缩的数据。事实上也还算好做,关键是有一点:如果数据未压缩,解压是无法得到有效数据。这一点可以用于区分数据是否压缩,而暂时不用添加 v1/v2 这样的数据结构版本号。

再次得到教训:优先做数据结构相关的事情。等数据结构做完善、做稳定了,UI、业务逻辑等内容,随意换。


博客原文:0910 - iPaste 搞定数据压缩

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 冬天说来就来,今天阳光不错,午后暖意融融,不知道是不是预示着今年会是一个暖冬?天一冷,早上就想喝一碗热乎乎的粥或者...
    小食光阅读 584评论 1 0
  • 今天上班特别的忙,心情也很烦躁,我又开始不淡定了,而且特别变得特别有情绪,下午X在的时候,我都冒火了,虽然不是针对...
    柴柴cc阅读 350评论 0 0