lzma从内存c ++压缩/解压缩

问题描述 投票:0回答:2

我有一个有效的代码,但是我对结果并不完全满意,所以我认为我可以在这里提出一些问题。

这是我的两个功能:

void compress(string nameSrc, string nameDst){

    ifstream input;
    input.open(nameSrc,fstream::in | fstream::binary);

    size_t propsSize = LZMA_PROPS_SIZE;
    size_t srcLen = getLength(input);
    size_t dstLen = srcLen; //??? no idea how to know to right value here

    unsigned char* src = new unsigned char[srcLen];
    unsigned char* dst = new unsigned char[dstLen + propsSize];

    input.read((char*)src, srcLen);

    int res = LzmaCompress(
        &dst[LZMA_PROPS_SIZE], &dstLen,
        src, srcLen,
        dst, &propsSize,
        -1, 0, -1, -1, -1, -1, -1);

    delete [] src;
    input.close();

    ofstream output(nameDst, ios::binary);
    output.write((char*)dst, dstLen + propsSize);

    delete [] dst;


}

和:

void unCompress(string nameSrc, string nameDst){

    ifstream input;
    input.open(nameSrc,fstream::in | fstream::binary);

    size_t srcLen = getLength(input);
    size_t dstLen = srcLen*5; //??? no idea how to know to right value here

    unsigned char* src = new unsigned char[srcLen];
    unsigned char* dst = new unsigned char[dstLen];

    input.read((char*)src,srcLen);

    int res = LzmaUncompress(dst,&dstLen,&src[LZMA_PROPS_SIZE],&srcLen, src, LZMA_PROPS_SIZE);

    delete [] src;
    input.close();

    ofstream output(nameDst, ios::binary);
    output.write((char*)dst, dstLen);

    delete [] dst;
}

  1. 在两个函数中,我应该如何知道要在dstLen中添加什么值?我不想为没有分配很多内存。
  2. 我必须强制转换为char *吗?我真的必须使用unsigned char吗?
  3. 我尝试更改LzmaCompress(numThreads)的最后一个参数,它没有改善性能,甚至没有改善。还有其他事情要做吗?
  4. 如果有任何提示,请随时告诉我。

谢谢。

c++ 7zip lzma
2个回答
1
投票

使用以下功能获取目的地尺寸:

INT32
EFIAPI
LzmaGetInfo(
CONST VOID  *Source,
UINT32      SourceSize,
UINT32      *DestinationSize
)
{
    UInt64  DecodedSize;

    ASSERT(SourceSize >= LZMA_HEADER_SIZE); (void)SourceSize;

    DecodedSize = GetDecodedSizeOfBuf((UINT8*)Source);

    *DestinationSize = (UINT32)DecodedSize;
    return ERR_SUCCESS;
}

0
投票

我认为您可以将源文件的长度保存到压缩文件中。解压缩时,可以通过压缩文件读取源文件的长度。

当然,您可以使用它进行压缩,这是我多次搜索的结果

dstLen = LZMA_PROPS_SIZE + srcLen + srcLen / 3 + 128

© www.soinside.com 2019 - 2024. All rights reserved.