snappy压缩/解压库

摘要:
snappysnappy是由google开发的压缩/解压C++库,注重压缩速度,压缩后文件大小比其它算法大一些snappy在64位x86并且是小端的cpu上性能最佳在Intel(R)Core(TM)22.4GHz中测试数据:压缩速率:~200-500MB/s解压速率:~400-800MB/s压缩比(压缩数据大小/原始数据大小):对于HTML:~25%对于普通文本(plaintext):~50%对于
snappy

snappy是由google开发的压缩/解压C++库,注重压缩速度,压缩后文件大小比其它算法大一些
snappy在64位x86并且是小端的cpu上性能最佳

  • 在Intel(R) Core(TM)2 2.4GHz中测试数据:
    压缩速率:~200-500MB/s
    解压速率:~400-800MB/s
  • 压缩比(压缩数据大小/原始数据大小):
    对于HTML:~25%
    对于普通文本(plain text):~50%
    对于JPEG等已经压缩过的文件:~100%
压缩/解压demo
/**
 * 压缩数据
 * @param bs 输入的字节数组
 * @return 经过压缩的数据
 */
Bytes SnappyCompress::compress(BytesConstRef bs) {
    // 提前分配足够的空间
    Bytes ret(snappy::MaxCompressedLength(bs.size()));
    size_t compressedLen = 0;

    // 进行压缩
    snappy::RawCompress(
        reinterpret_cast<const char*>(bs.data()),
        bs.size(),
        reinterpret_cast<char*>(ret.data()),
        &compressedLen
    );

    // 调整为实际的压缩长度
    ret.resize(compressedLen);

    return ret;
}

/**
 * 解压数据
 * @param bs 经过压缩的字节数组
 * @return 经过解压的数据
 * @throw 输入的压缩数据损坏抛出CorruptedInput异常
 */
Bytes SnappyCompress::uncompress(BytesConstRef bs) {
    // 解析出解压数据的长度(花费O(1)时间)
    size_t uncompressedLen = 0;
    bool status = snappy::GetUncompressedLength(
        reinterpret_cast<const char*>(bs.data()),
        bs.size(),
        &uncompressedLen
    );

    if (!status) {
        // 解析长度编码出错
        throw CorruptedInput();
    }

    // 提前分配空间
    Bytes ret(uncompressedLen);

    // 进行解压
    status = snappy::RawUncompress(
        reinterpret_cast<const char*>(bs.data()),
        bs.size(),
        reinterpret_cast<char*>(ret.data())
    );

    if (!status) {
        // 压缩数据损坏
        throw CorruptedInput();
    }

    return ret;
}

免责声明:文章转载自《snappy压缩/解压库》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Weblogic的安装、配置与应用部署sql server 的内置账户下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

SQL SERVER性能分析死锁检测数据库阻塞语句&amp;lt;转&amp;gt;

工作中数据库经常出现内存,找了篇文章 参照CSDN,中国风(Roy)一篇死锁文章 阻塞:其中一个事务阻塞,其它事务等待对方释放它们的锁,同时会导致死锁问题。 整理人:中国风(Roy) 参照Roy_88的博客 http://blog.csdn.net/roy_88/archive/2008/07/21/2682044.aspx 日期:2008.07.20...

Oracle中的日期和字符串互相转换

转载出处:http://blog.sina.com.cn/s/blog_44a005380100k6rv.html TO_DATE格式(以时间:2007-11-02   13:45:25为例)            Year:              yy two digits 两位年                显示值:07         yyy...

oracle语句,时分秒格式转成秒

select (hours+minutes+seconds) AS total_seconds from( selectto_char(sysdate,('HH24'))*60*60 AShours, to_char(sysdate,('mi'))*60 ASminutes, to_char(sysdate,'ss') ASseconds fromA );...

oracle中to_date详细用法示例(oracle日期格式转换)

1. 日期和字符转换函数用法(to_date,to_char) select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual; //日期转化为字符串select to_char(sysdate,'yyyy') as nowYear from dual; //获取时间的年selec...

curl实现SFTP上传下载文件

摘自:https://blog.csdn.net/swj9099/article/details/85292444 #include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <pthread.h...

ASCII 非打印字符

项目出了问题,因为AscII非打印字符的原因,后来找了一下啊ASCII的非打印字符,总共有31个,然后我们直接全部替换成问号了. 解决方式为先找到非打印字符,这是我从网上找的非打印字符表: 进制 十六进制 字符   十进制 十六进制 字符 0 00 空   16 10 数据链路转意 1 01 头标开始   17 11 设备控制 1 2 02...