iOS压缩算法简单比较

2017-08-07  本文已影响153人  晚雪浓情

简单使用了一下LAFSEMiniLZO以及系统自带的zlib

发现在重复数据非常大的情况下LAFSE的表现很优秀,291w个字节能压缩到3.2k左右。

在随便找了一首歌(mp3格式)并读出它的Data测试的情况下,MiniLZO的压缩率能稍高于LZFSE

调用方法封装⬇️

func lzfse_encode(src:Data,dst:inout Data){
    src.withUnsafeBytes { (fromBytes: UnsafePointer<UInt8>) -> Void in
        dst.withUnsafeMutableBytes{ (toBytes: UnsafeMutablePointer<UInt8>) -> Void in
            let res = lzfse_encode_buffer(toBytes, dst.count, fromBytes, src.count, nil)
            if res > 0{
                dst = dst[0...res]
            }
            print("res == \(res)")
        }
    }
}

func lzfse_decode(src:Data,dst:inout Data){
    src.withUnsafeBytes { (fromBytes: UnsafePointer<UInt8>) -> Void in
        dst.withUnsafeMutableBytes{ (toBytes: UnsafeMutablePointer<UInt8>) -> Void in
            let res = lzfse_decode_buffer(toBytes, dst.count, fromBytes, src.count, nil)
            print("res == \(res)")
        }
    }
}

func minilzo_encode(src:Data,dst:inout Data){
    src.withUnsafeBytes { (fromBytes: UnsafePointer<UInt8>) -> Void in
        dst.withUnsafeMutableBytes{ (toBytes: UnsafeMutablePointer<UInt8>) -> Void in
            var length = lzo_uint(dst.count)
            let wkrmem = UnsafeMutableRawPointer.allocate(bytes: src.count, alignedTo: 0)
            let res = lzo1x_1_compress(fromBytes, lzo_uint(src.count), toBytes, &length, wkrmem)
            if res == LZO_E_OK{
                print("res == \(length)")
            }
        }
    }
}

func minilzo_decode(src:Data,dst:inout Data){
    src.withUnsafeBytes { (fromBytes: UnsafePointer<UInt8>) -> Void in
        dst.withUnsafeMutableBytes{ (toBytes: UnsafeMutablePointer<UInt8>) -> Void in
            var length = lzo_uint(src.count)
            let res = lzo1x_decompress(fromBytes, lzo_uint(src.count), toBytes, &length, nil)
            if res == LZO_E_OK{
                print("res == \(length)")
            }
        }
    }
}

func zlib_encode(src:Data,dst:inout Data){
    src.withUnsafeBytes { (fromBytes: UnsafePointer<Bytef>) -> Void in
        dst.withUnsafeMutableBytes{ (toBytes: UnsafeMutablePointer<Bytef>) -> Void in
            var length = uLongf(src.count)
            let res = compress(toBytes, &length, fromBytes, uLong(src.count))
            if res == Z_OK{
                print("res == \(length)")
            }
        }
    }
}

func zlib_decode(src:Data,dst:inout Data){
    src.withUnsafeBytes { (fromBytes: UnsafePointer<UInt8>) -> Void in
        dst.withUnsafeMutableBytes{ (toBytes: UnsafeMutablePointer<UInt8>) -> Void in
            var length = uLongf(dst.count)
            let res = uncompress(toBytes, &length, fromBytes, uLong(src.count))
            if res == Z_OK{
                print("res == \(length)")
            }
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读