博主主要使用Windows,以下操作均为windows

  1. 从官网下载库-》https://www.7-zip.org/sdk.html

image.png

  1. 解压到一个文件夹image.png
  2. 使用Visual studio 的命令窗口来执行命令image.png

E:库lzma2106CUtilLzmaLib----这是我当前的路径image.png使用命令“nmake NEW_COMPILER=1 MY_STATIC_LINK=1”产生7zra.dll和7zra.lib文件在当前文件夹【如果需要x86请使用 VS的x86 Native Tools 编译】image.pngimage.png

vs使用

添加头文件,在exe运行路径下放入生成的.dll文件。

image.png

image.png

image.png

//简易代码

unsigned char g_prop[5] =
{
    0
};
//压缩的信息返回出去
typedef struct COMPRESSINFO
{
    //预估压缩后的字节数  【压缩后的字节数】
    DWORD Retcompress_size;
    //文件原来的大小
    DWORD SrcFile_size;
    //返回的指针
    char* pRetNewBuffer;

}CompressInfo, *pCompressInfo;



// 压缩
void LZMAcompress(const char* _data, DWORD dwSize, pCompressInfo* RetInfo)
{


    unsigned propsSize = LZMA_PROPS_SIZE;
    size_t sizeProp = 5;
    size_t destLen = dwSize;                        //压缩后的大小  暂时

    unsigned char* pLzma = new unsigned char[destLen]; //存放压缩数据

    //compressBound    destLen 压缩后会更改
    int nRet = LzmaCompress(pLzma, &destLen, (unsigned char*)_data, dwSize, g_prop,
        &sizeProp, 9, (1 << 24), 3, 0, 2, 32, 2);
    if (nRet==SZ_ERROR_OUTPUT_EOF)
    {
        //出错了
        delete[] pLzma;
        destLen += dwSize;
        pLzma = new unsigned char[destLen]; //存放压缩数据
        int nRet = LzmaCompress(pLzma, &destLen, (unsigned char*)_data, dwSize, g_prop,
            &sizeProp, 9, (1 << 24), 3, 0, 2, 32, 2);
    }
    if (nRet != SZ_OK) 
    {
        return;
    }
    char* RetData = new char[destLen] {0};
    memcpy(RetData, pLzma, destLen);
    delete pLzma;

    printf("压缩前:%d , 压缩后:%d , 压缩比:%.2lf%%\n", dwSize, destLen, destLen * 1.0 / dwSize * 100.0);
    (*RetInfo)->pRetNewBuffer = (char*)RetData;
    (*RetInfo)->Retcompress_size = destLen;
    (*RetInfo)->SrcFile_size = dwSize;

}


//解压
void LZMAuncompress(
    unsigned char **outBuf, size_t *dstLen,
    unsigned char *inBuf, size_t srcLen
    ) {

    size_t nDesLen = *dstLen;
    *outBuf = new unsigned char[nDesLen] {0};

    DWORD dwOut=0;
    int res = LzmaUncompress(
        *outBuf, &nDesLen,
        inBuf, &srcLen,
        g_prop, LZMA_PROPS_SIZE);

}


//调用
//注意 g_prop一定是压缩变更的值否则可能出错

      pCompressInfo LZMANewFileInfo = new CompressInfo{ 0 };        //LZMA
      LZMAcompress((char*)g_tempPixels, size, &LZMANewFileInfo);

      size_t  decSize= LZMANewFileInfo->SrcFile_size;
      unsigned char *data, *dec = NULL;
      LZMAuncompress(&dec, &decSize, (unsigned char*)LZMANewFileInfo->pRetNewBuffer, LZMANewFileInfo->Retcompress_size);
最后修改:2021 年 12 月 01 日 01 : 02 PM