Self-encapsulated LZMA interface library for unity, including dll of win platform, so dynamic library of Android and. a static library of iOS
LZMA is currently the highest compression ratio compression algorithm. Official versions of LZMA are available in many languages. unity can use C# directly, but it has been proved that the compression speed of pure C version is four or five times faster than that of C#.
Introduction to Packaging Engineering
LZMA (Xcode) is a static library project of iOS. The corresponding. a static library can be obtained directly by run ning in xcode. The top-level interface is encapsulated in LZMALib.c.
LZMA (Android Studio) is an Android project that supports runtime C++. The top-level interface is encapsulated in LZMALib.c. Build the corresponding. so library into the APK package, decompressing the APK can get the inside LZMA so library (apk path:. \ app build outputs apk debug).
7zSDK(VS) is the official and complete SDK and source code project of LZMA. The project of compiling LZMALib dynamic library is one of LZMALib projects. The path is:. C Util LzmaLib. The top-level interface is encapsulated in LZMALib.c.
Preview of Packaging Interface
The top layer encapsulates two interfaces for compression and decompression. The compression parameters are encapsulated in the interface by default. If you want to change, you need to modify and compile a new library in the corresponding encapsulation project.
// Custom interface
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, unsigned char *dest, size_t *destLen);
//MY_STDAPI LZMAAdd(int x);//Test
/* 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, size_t *destLen, const unsigned char *src, size_t *srcLen,
const unsigned char *props, size_t propsSize)
{
ELzmaStatus status;
return LzmaDecode(dest, destLen, src, srcLen, props, (unsigned)propsSize, LZMA_FINISH_ANY, &status, &g_Alloc);
}
// Custom interface
MY_STDAPI DefaultLzmaCompress(const unsigned char *src, const size_t srcLen, unsigned char *dest, 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, destLen, src, srcLen, outProps, &outPropsSize, level, dictSize, lc, lp, pb, fb, numThreads);
}
MY_STDAPI DefaultLzmaUncompress(const unsigned char *src, size_t *srcLen, unsigned char *dest, size_t *destLen)
{
return LzmaUncompress(dest, destLen, src, srcLen, outProps, outPropsSize);
}
MY_STDAPI LZMAAdd(int x)
{
return x + 1;
}
Examples of usage methods
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, byte[] dest, ref int destLen);
// Use this for initialization
void Start () {
// compress
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, srcLen, dest, ref destLen); // Successful compressRes returns 0, and the value of destLen becomes the compressed data length.
// decompression
byte[] unsrc = new byte[destLen];
for (int i = 0; i < destLen; i++) {
unsrc[i] = dest[i]; // Copy the compressed dest data to the decompressed src data buffer
}
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); // The output of successful decompression should be the same as the input value
}
// Update is called once per frame
void Update () {
}
}