欢迎访问我的个人博客 ,原文链接
Zstandard(缩写为Zstd)是由Facebook的Yann Collet开发的一个无损数据压缩算法。Zstandard在设计上与DEFLATE(.zip、gzip)算法有着差不多的压缩比,但有更高的压缩和解压缩速度。
Zstandard使用字典算法(LZ77)结合熵编码法的有限状态熵(tANS)。—–Wikipedia
Compressor name | Ratio | Compression | Decompress. |
---|---|---|---|
zstd 1.1.3 -1 | 2.877 | 430 MB/s | 1110 MB/s |
zlib 1.2.8 -1 | 2.743 | 110 MB/s | 400 MB/s |
brotli 0.5.2 -0 | 2.708 | 400 MB/s | 430 MB/s |
quicklz 1.5.0 -1 | 2.238 | 550 MB/s | 710 MB/s |
lzo1x 2.09 -1 | 2.108 | 650 MB/s | 830 MB/s |
lz4 1.7.5 | 2.101 | 720 MB/s | 3600 MB/s |
snappy 1.1.3 | 2.091 | 500 MB/s | 1650 MB/s |
lzf 3.6 -1 | 2.077 | 400 MB/s | 860 MB/s |
很简单,进入\build\VS_scripts目录下根据不同环境运行相应的脚本即可!
- 在项目中包含头文件#include “zstd/zstd.h”
- #pragma comment(lib,”libzstd.lib”);
- 在项目exe目录下面加入libzstd.dll文件
- 在项目lib文件夹(或者其他什么文件夹)加入libzstd.lib
/*! ZSTD_compress() :
* Compresses `src` content as a single zstd compressed frame into already allocated `dst`.
* Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`.
* @return : compressed size written into `dst` (<= `dstCapacity),
* or an error code if it fails (which can be tested using ZSTD_isError()). */
ZSTDLIB_API size_t ZSTD_compress( void* dst, size_t dstCapacity,
const void* src, size_t srcSize,
int compressionLevel);
/*! ZSTD_decompress() :
* `compressedSize` : must be the _exact_ size of some number of compressed and/or skippable frames.
* `dstCapacity` is an upper bound of originalSize.
* If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data.
* @return : the number of bytes decompressed into `dst` (<= `dstCapacity`),
* or an errorCode if it fails (which can be tested using ZSTD_isError()). */
ZSTDLIB_API size_t ZSTD_decompress( void* dst, size_t dstCapacity,
const void* src, size_t compressedSize);
ZSTDLIB_API unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize);
/*!< maximum compressed size in worst case scenario */
ZSTDLIB_API size_t ZSTD_compressBound(size_t srcSize);
压缩流程:
- 使用ZSTD_compressBound获取最大压缩缓冲区大小,然后申请缓冲区;
- 使用ZSTD_compress进行压缩
//可以设定压缩等级 1 up to ZSTD_maxCLevel()
size_t dstSize = ZSTD_compress(pDest, szBufSize, pSource, szSrcSize, iCompressLevel);
if (ZSTD_isError(dstSize))
{
return -1;
}
return dstSize;
解压流程:
- 可以使用ZSTD_getDecompressedSize来获取压缩后最大缓冲区大小;来申请解压缓冲区;
size_t dstSize = ZSTD_decompress(pDest, szBufSize, pSource, szSrcSize);
if (ZSTD_isError(dstSize))
{
return -1;
}
return dstSize;