1.简介
LZ4是一种快速的压缩算法,提供压缩和解压缩的速度,而牺牲了压缩率。它被设计用于快速的数据压缩和解压缩,特别是用于数据存储和传输。LZ4通常用于需要高速数据处理的场景,如数据库、日志文件处理和实时数据传输。
LZ4的特点是压缩和解压缩速度快,但压缩率不如其他一些压缩算法(如gzip或zstd)。
LZ4的工作原理是基于字典的压缩,它查找数据中的重复字符串,并用较短的形式来表示这些重复的部分。这种算法非常适合于有大量重复数据的情况。
以下是和其他压缩算法的比较:
2.环境搭建
下载地址:https://github.com/lz4/lz4/tree/v1.9.4
解压缩文件之后,进入到编译目录。
双击lz4.sln打开工程。
这里编译静态库,将lz4file.c和lz4file.h添加进工程中,编译好后生成liblz4_static.lib静态库。
拷贝include文件和生成的liblz4_static.lib静态库到我们的demo工程下。
最后在visual studio中添加include目录 和 lib目录即可。
配置visual studio环境,具体参考请看Jsoncpp介绍。
3.示例
压缩/解压缩文件
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
extern "C"
{
#include "lz4.h"
#include "lz4file.h"
}
#define CHUNK_SIZE (16*1024)
static size_t get_file_size(char *filename)
{
struct stat statbuf;
if (filename == NULL) {
return 0;
}
if (stat(filename, &statbuf)) {
return 0;
}
return statbuf.st_size;
}
static int compress_file(FILE* f_in, FILE* f_out)
{
assert(f_in != NULL); assert(f_out != NULL);
LZ4F_errorCode_t ret = LZ4F_OK_NoError;
size_t len;
LZ4_writeFile_t* lz4fWrite;
void* const buf = malloc(CHUNK_SIZE);
if (!buf) {
printf("error: memory allocation failed \n");
}
/* Of course, you can also use prefsPtr to
* set the parameters of the compressed file
* NULL is use default
*/
ret = LZ4F_writeOpen(&lz4fWrite, f_out, NULL);
if (LZ4F_isError(ret)) {
printf("LZ4F_writeOpen error: %s\n", LZ4F_getErrorName(ret));
free(buf);
return 1;
}
while (1) {
len = fread(buf, 1, CHUNK_SIZE, f_in);
if (ferror(f_in)) {
printf("fread error\n");
goto out;
}
/* nothing to read */
if (len == 0) {
break;
}
ret = LZ4F_write(lz4fWrite, buf, len);
if (LZ4F_isError(ret)) {
printf("LZ4F_write: %s\n", LZ4F_getErrorName(ret));
goto out;
}
}
out:
free(buf);
if (LZ4F_isError(LZ4F_writeClose(lz4fWrite))) {
printf("LZ4F_writeClose: %s\n", LZ4F_getErrorName(ret));
return 1;
}
return 0;
}
static int decompress_file(FILE* f_in, FILE* f_out)
{
assert(f_in != NULL); assert(f_out != NULL);
LZ4F_errorCode_t ret = LZ4F_OK_NoError;
LZ4_readFile_t* lz4fRead;
void* const buf = malloc(CHUNK_SIZE);
if (!buf) {
printf("error: memory allocation failed \n");
}
ret = LZ4F_readOpen(&lz4fRead, f_in);
if (LZ4F_isError(ret)) {
printf("LZ4F_readOpen error: %s\n", LZ4F_getErrorName(ret));
free(buf);
return 1;
}
while (1) {
ret = LZ4F_read(lz4fRead, buf, CHUNK_SIZE);
if (LZ4F_isError(ret)) {
printf("LZ4F_read error: %s\n", LZ4F_getErrorName(ret));
goto out;
}
/* nothing to read */
if (ret == 0) {
break;
}
if (fwrite(buf, 1, ret, f_out) != ret) {
printf("write error!\n");
goto out;
}
}
out:
free(buf);
if (LZ4F_isError(LZ4F_readClose(lz4fRead))) {
printf("LZ4F_readClose: %s\n", LZ4F_getErrorName(ret));
return 1;
}
if (ret) {
return 1;
}
return 0;
}
int compareFiles(FILE* fp0, FILE* fp1)
{
int result = 0;
while (result == 0) {
char b0[1024];
char b1[1024];
size_t const r0 = fread(b0, 1, sizeof(b0), fp0);
size_t const r1 = fread(b1, 1, sizeof(b1), fp1);
result = (r0 != r1);
if (!r0 || !r1) break;
if (!result) result = memcmp(b0, b1, r0);
}
return result;
}
int main(int argc, const char **argv)
{
char inpFilename[256] = { 0 };
char lz4Filename[256] = { 0 };
char decFilename[256] = { 0 };
if (argc < 2) {
printf("Please specify input filename\n");
return 0;
}
snprintf(inpFilename, 256, "%s", argv[1]);
snprintf(lz4Filename, 256, "%s.lz4", argv[1]);
snprintf(decFilename, 256, "%s.lz4.dec", argv[1]);
printf("inp = [%s]\n", inpFilename);
printf("lz4 = [%s]\n", lz4Filename);
printf("dec = [%s]\n", decFilename);
/* compress */
{
FILE* const inpFp = fopen(inpFilename, "rb");
FILE* const outFp = fopen(lz4Filename, "wb");
printf("compress : %s -> %s\n", inpFilename, lz4Filename);
LZ4F_errorCode_t ret = compress_file(inpFp, outFp);
fclose(inpFp);
fclose(outFp);
if (ret) {
printf("compression error: %s\n", LZ4F_getErrorName(ret));
return 1;
}
printf("%s: %zu → %zu bytes, %.1f%%\n",
inpFilename,
get_file_size(inpFilename),
get_file_size(lz4Filename), /* might overflow is size_t is 32 bits and size_{in,out} > 4 GB */
(double)get_file_size(lz4Filename) / get_file_size(inpFilename) * 100);
printf("compress : done\n");
}
/* decompress */
{
FILE* const inpFp = fopen(lz4Filename, "rb");
FILE* const outFp = fopen(decFilename, "wb");
printf("decompress : %s -> %s\n", lz4Filename, decFilename);
LZ4F_errorCode_t ret = decompress_file(inpFp, outFp);
fclose(outFp);
fclose(inpFp);
if (ret) {
printf("compression error: %s\n", LZ4F_getErrorName(ret));
return 1;
}
printf("decompress : done\n");
}
/* verify */
{ FILE* const inpFp = fopen(inpFilename, "rb");
FILE* const decFp = fopen(decFilename, "rb");
printf("verify : %s <-> %s\n", inpFilename, decFilename);
int const cmp = compareFiles(inpFp, decFp);
fclose(decFp);
fclose(inpFp);
if (cmp) {
printf("corruption detected : decompressed file differs from original\n");
return cmp;
}
printf("verify : OK\n");
}
return 0;
}
生成可执行程序,添加两个参数。运行效果如下:
4.更多参考
libVLC 专栏介绍-CSDN博客
Qt+FFmpeg+opengl从零制作视频播放器-1.项目介绍_qt opengl视频播放器-CSDN博客
QCharts -1.概述-CSDN博客
Zlib介绍
压缩和归档库-miniz介绍