用于Unity(windows\iOS\安卓)的纯C版LZMA压缩算法库(dl
封装工程介绍
封装接口预览最顶层封装了两个接口,用于压缩和解压,压缩参数默认封在了接口内,如果要更改需要在相应的封装工程中修改并编译出新库: // 自定义接口
MY_STDAPI DefaultLzmaCompress(const unsigned char *src,const size_t srcLen,unsigned char *dest,size_t *destLen);
MY_STDAPI DefaultLzmaUncompress(const unsigned char *src,size_t *srcLen,size_t *destLen);
//MY_STDAPI LZMAAdd(int x); // 测试用
/* LzmaLib.c -- LZMA library wrapper #include "Alloc.h" #include "LzmaDec.h" #include "LzmaEnc.h" #include "LzmaLib.h" unsigned char outProps[LZMA_PROPS_SIZE]; size_t outPropsSize = LZMA_PROPS_SIZE; MY_STDAPI LzmaCompress(unsigned char *dest,size_t *destLen,const unsigned char *src,size_t srcLen,unsigned char *outProps,size_t *outPropsSize,int level,/* 0 <= level <= 9,default = 5 */
unsigned dictSize,/* use (1 << N) or (3 << N). 4 KB < dictSize <= 128 MB */
int lc,/* 0 <= lc <= 8,default = 3 */
int lp,/* 0 <= lp <= 4,default = 0 */
int pb,/* 0 <= pb <= 4,default = 2 */
int fb,/* 5 <= fb <= 273,default = 32 */
int numThreads /* 1 or 2,default = 2 */
)
{
CLzmaEncProps props;
LzmaEncProps_Init(&props);
props.level = level;
props.dictSize = dictSize;
props.lc = lc;
props.lp = lp;
props.pb = pb;
props.fb = fb;
props.numThreads = numThreads;
return LzmaEncode(dest,destLen,src,srcLen,&props,outProps,outPropsSize,0,NULL,&g_Alloc,&g_Alloc);
}
MY_STDAPI LzmaUncompress(unsigned char *dest,const unsigned char *src,const unsigned char *props,size_t propsSize)
{
ELzmaStatus status;
return LzmaDecode(dest,props,(unsigned)propsSize,LZMA_FINISH_ANY,&status,&g_Alloc);
}
// 自定义接口
MY_STDAPI DefaultLzmaCompress(const unsigned char *src,size_t *destLen)
{
int level = 5;
unsigned dictSize = 1 << 23;
int lc = 3;
int lp = 0;
int pb = 2;
int fb = 128;
int numThreads = 1;
return LzmaCompress(dest,&outPropsSize,level,dictSize,lc,lp,pb,fb,numThreads);
}
MY_STDAPI DefaultLzmaUncompress(const unsigned char *src,size_t *destLen)
{
return LzmaUncompress(dest,outPropsSize);
}
MY_STDAPI LZMAAdd(int x)
{
return x + 1;
}
使用方法示例using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
using UnityEngine.UI;
public class Compress : MonoBehaviour {
public Text text = null;
[DllImport("LZMA")]
public static extern int LZMAAdd(int x);
[DllImport("LZMA")]
public static extern int DefaultLzmaCompress(byte[] src,int srcLen,byte[] dest,ref int destLen);
[DllImport("LZMA")]
public static extern int DefaultLzmaUncompress(byte[] src,ref int srcLen,ref int destLen);
// Use this for initialization
void Start () {
// 压缩
string input = "122333444455555666666777777788888888999999999";
byte[] src = System.Text.Encoding.Default.GetBytes(input);
byte[] dest = new byte[src.Length];
int srcLen = src.Length;
int destLen = dest.Length;
int compressRes = DefaultLzmaCompress(src,dest,ref destLen); // 压缩成功compressRes会返回0,destLen的值会变成压缩后的数据长度
// 解压
byte[] unsrc = new byte[destLen];
for (int i = 0; i < destLen; i++) {
unsrc[i] = dest[i]; // 将压缩后得到的dest数据复制到解压的src数据缓冲中
}
srcLen = destLen;
destLen = src.Length;
byte[] undest = new byte[destLen];
int uncompressRes = DefaultLzmaUncompress(unsrc,ref srcLen,undest,ref destLen);
string output = System.Text.Encoding.Default.GetString(undest); // 解压成功output应该和input值相同
}
// Update is called once per frame
void Update () {
}
}
工程地址(包括编译好的各平台的库,各平台的库工程和使用示例)https://github.com/jiangxh1992/XHLZMA (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- windows-server-2008 – Active Directory用户和
- Windows在运行长C Visual Studio程序时会休眠
- windows与虚拟机的linux共享windows下的一个文件
- 将函数指针作为参数传递给dll函数并从dll内部调用
- WiX:InstallUISequence和InstallExecuteSequenc
- Windows开发人员权限?
- .net – 为什么我无法在2.0.0.0播放器框架中重用
- Windows – 如何虚拟化SharePoint ASP应用程序
- .net – 加载Azure Storage 2.0时出错 – 无法加
- Windows中的头文件alloca.h