当前位置: 首页 > news >正文

lz4库使用

源码:https://github.com/lz4/lz4

三种编译方式

  1. lz4.h、lz4.c、simple_buffer.c(源文件编译)

  2. lz4.h、simple_buffer.c、liblz4_static.lib(调用静态库)

  3. lz4.h、simple_buffer.c、liblz4.lib(调用动态库)

第一种:

优点:不用编译库,直接复制lz4.h、lz4.两个文件到项目中即可.simple_buffer.c是调用案例,不用拷贝.

windows编译

工程相对路径lz4\build\VS2017\lz4.sln直接编译库即可

Linux环境

cd ~/lz4/programs

make clean

Make

./lz4 README.md(压缩文件)

./lz4 -d README.md.lz4(解压文件)

优点:不用编译库,直接复制lz4.h、lz4.两个文件到项目中即可.

windows/linux测试可用 

#-------------------------------------------------
#
# Project created by QtCreator 2021-08-27T09:23:17
#
#-------------------------------------------------QT       += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = lz4
TEMPLATE = appCONFIG += c++11SOURCES += main.cpp\mainwindow.cpp \lz4.cHEADERS  += mainwindow.h \lz4.hFORMS    += mainwindow.ui

 对一个文件进行压缩

#include "mainwindow.h"
#include <QApplication>
#include "lz4.h"
#include <fstream>int main(int argc, char *argv[])
{QApplication a(argc, argv);std::ifstream iFile("E:/20210302113553.bak",std::ios::binary);int desize = 0;//记录压缩前的数据if(iFile){//获取压缩数据iFile.seekg(0,iFile.end);//移动到文件结尾int size = iFile.tellg();//返回当前位置iFile.seekg (0);char * buffer = new char [size];memset(buffer,0,size);iFile.read (buffer,size);desize = size;//解压和压缩前数据大小一样//计算压缩后数据最大大小(数据不可压缩时)const int max_dst_size = LZ4_compressBound(size);if(max_dst_size > LZ4_MAX_INPUT_SIZE|| max_dst_size<=0)//未找到合适大小return 0;//压缩后的内存char* compressed_data = new char[(size_t)max_dst_size];memset(compressed_data,0,max_dst_size);if (compressed_data == NULL)return 0;//压缩数据const int compressed_data_size = LZ4_compress_default(buffer, compressed_data, size, max_dst_size);if (compressed_data_size <= 0)return 0;//压缩后数据写入文件std::ofstream oFile("E:/20210302113553_.bak",std::ios::binary);//必须指定而二进制if(oFile){oFile.write(compressed_data,compressed_data_size);}oFile.close();iFile.close();delete[] buffer;delete[] compressed_data;//释放内存}//解压std::ifstream iFiledecompressed("E:/20210302113553_.bak",std::ios::binary);//必须指定而二进制,否则会导致数据长度不一致if(iFiledecompressed.is_open()){if (iFiledecompressed.eof()){iFiledecompressed.clear();}//获取压缩后的数据iFiledecompressed.seekg(0,iFiledecompressed.end);//移动到文件结尾int size = iFiledecompressed.tellg();//返回当前位置iFiledecompressed.seekg (0);char * buffer = new char [size];memset(buffer,0,size);iFiledecompressed.read(buffer,size);//解压后的内存char * debuffer = new char [desize];memset(debuffer,0,desize);//解压int decompressed_size = LZ4_decompress_safe(buffer,debuffer,size,desize);if(decompressed_size<0){return 0;}//解压后数据写入文件std::ofstream oFile("E:/20210302113553__.bak",std::ios::binary);if(oFile){oFile.write(debuffer,desize);}iFiledecompressed.close();oFile.close();delete[] buffer;delete[] debuffer;}MainWindow w;w.show();return a.exec();
}

官方的例子- simple_buffer.c

/*
* simple_buffer.c
* Copyright  : Kyle Harper
* License    : Follows same licensing as the lz4.c/lz4.h program at any given time.  Currently, BSD 2.
* Description: Example program to demonstrate the basic usage of the compress/decompress functions within lz4.c/lz4.h.
*              The functions you'll likely want are LZ4_compress_default and LZ4_decompress_safe.
*              Both of these are documented in the lz4.h header file; I recommend reading them.
*//* Dependencies */
#include <stdio.h>   // For printf()
#include <string.h>  // For memcmp()
#include <stdlib.h>  // For exit()
#include "lz4.h"     // This is all that is required to expose the prototypes for basic compression and decompression./*
* Simple show-error-and-bail function.
*/
void run_screaming(const char* message, const int code) {printf("%s \n", message);exit(code);
}/*
* main
*/
int main(void) {/* Introduction */// Below we will have a Compression and Decompression section to demonstrate.// There are a few important notes before we start://   1) The return codes of LZ4_ functions are important.//      Read lz4.h if you're unsure what a given code means.//   2) LZ4 uses char* pointers in all LZ4_ functions.//      This is baked into the API and not going to change, for consistency.//      If your program uses different pointer types,//      you may need to do some casting or set the right -Wno compiler flags to ignore those warnings (e.g.: -Wno-pointer-sign)./* Compression */// We'll store some text into a variable pointed to by *src to be compressed later.const char* const src = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor site amat.";// The compression function needs to know how many bytes exist.  Since we're using a string, we can use strlen() + 1 (for \0).const int src_size = (int)(strlen(src) + 1);// LZ4 provides a function that will tell you the maximum size of compressed output based on input data via LZ4_compressBound().const int max_dst_size = LZ4_compressBound(src_size);// We will use that size for our destination boundary when allocating space.char* compressed_data = malloc((size_t)max_dst_size);if (compressed_data == NULL)run_screaming("Failed to allocate memory for *compressed_data.", 1);// That's all the information and preparation LZ4 needs to compress *src into *compressed_data.// Invoke LZ4_compress_default now with our size values and pointers to our memory locations.// Save the return value for error checking.const int compressed_data_size = LZ4_compress_default(src, compressed_data, src_size, max_dst_size);// Check return_value to determine what happened.if (compressed_data_size <= 0)run_screaming("A 0 or negative result from LZ4_compress_default() indicates a failure trying to compress the data. ", 1);if (compressed_data_size > 0)printf("We successfully compressed some data! Ratio: %.2f\n",(float) compressed_data_size/src_size);// Not only does a positive return_value mean success, the value returned == the number of bytes required.// You can use this to realloc() *compress_data to free up memory, if desired.  We'll do so just to demonstrate the concept.compressed_data = (char *)realloc(compressed_data, (size_t)compressed_data_size);if (compressed_data == NULL)run_screaming("Failed to re-alloc memory for compressed_data.  Sad :(", 1);/* Decompression */// Now that we've successfully compressed the information from *src to *compressed_data, let's do the opposite!// The decompression will need to know the compressed size, and an upper bound of the decompressed size.// In this example, we just re-use this information from previous section,// but in a real-world scenario, metadata must be transmitted to the decompression side.// Each implementation is in charge of this part. Oftentimes, it adds some header of its own.// Sometimes, the metadata can be extracted from the local context.// First, let's create a *new_src location of size src_size since we know that value.char* const regen_buffer = malloc(src_size);if (regen_buffer == NULL)run_screaming("Failed to allocate memory for *regen_buffer.", 1);// The LZ4_decompress_safe function needs to know where the compressed data is, how many bytes long it is,// where the regen_buffer memory location is, and how large regen_buffer (uncompressed) output will be.// Again, save the return_value.const int decompressed_size = LZ4_decompress_safe(compressed_data, regen_buffer, compressed_data_size, src_size);free(compressed_data);   /* no longer useful */if (decompressed_size < 0)run_screaming("A negative result from LZ4_decompress_safe indicates a failure trying to decompress the data.  See exit code (echo $?) for value returned.", decompressed_size);if (decompressed_size >= 0)printf("We successfully decompressed some data!\n");// Not only does a positive return value mean success,// value returned == number of bytes regenerated from compressed_data stream.if (decompressed_size != src_size)run_screaming("Decompressed data is different from original! \n", 1);/* Validation */// We should be able to compare our original *src with our *new_src and be byte-for-byte identical.if (memcmp(src, regen_buffer, src_size) != 0)run_screaming("Validation failed.  *src and *new_src are not identical.", 1);printf("Validation done. The string we ended up with is:\n%s\n", regen_buffer);return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.dtcms.com/a/258817.html

相关文章:

  • 洛谷P1092 [NOIP 2004 提高组] 虫食算
  • 29.设计模式的选择与应用
  • windows 上 build 时,微软给出的 vcpkg 工具,如何使用
  • 关于数据编码、进制、位运算的详细讲解(从属GESP三级)
  • C#调用MATLAB函数
  • [Linux] Linux用户和组管理
  • 用福昕阅读器打开pdf文件,整个程序窗口自动缩小的问题
  • Python邮件自动化完全指南:从基础到高级应用
  • 如何通过nvm切换本地node环境详情教程(已装过node.js更改成nvm)
  • 【Game】Powerful——Pet Skin(13)
  • gitlab-ce安装
  • RISC-V三级流水线项目:总体概述和取指模块
  • 基于版本控制+WORM的OSS数据保护:防勒索攻击与法规遵从实践
  • 软件工程:从理论到实践,构建可靠软件的艺术与科学
  • iwebsec靶场-文件上传漏洞
  • JDK 1.8 Stream API:集合流处理深度解析
  • SQL关键字三分钟入门:UPDATE —— 修改数据
  • C++ 快速回顾(一)
  • 覆盖迁移工具选型、增量同步策略与数据一致性校验
  • 用字符打印中文字“里”
  • 芸众商城系统部署教程 接口报错500 芸众商城队列安装启动教程
  • Javaweb - 5 事件的绑定
  • Sping AI接入deepseek
  • 微信小程序中 rpx与px的区别
  • 【FreeRTOS】FreeRTOS源码概述
  • HSA22HSA29美光固态芯片D8BJVC8BJW
  • 机器学习×第十三卷:集成学习上篇——她不再独断,而是召集小队贴贴你
  • 多线程的同步
  • k8s强制删除podpvpvc和nsnamespace
  • 人机交互动画制作新突破!文本驱动扩散框架HOIDiNi:一句话驱动虚拟人高精度操作物体。