1

我有一个代码工作,但我对结果并不完全满意,所以我想我可以在这里问一些问题。

这是我的两个功能:

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. 如果您有任何提示,请随时告诉我。

谢谢。

4

2 回答 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;
}
于 2014-12-07T12:08:18.320 回答
0

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

当然,你可以用这个来压缩,这是我多次搜索的结果

dstLen = LZMA_PROPS_SIZE + srcLen + srcLen/3 + 128

于 2020-01-03T10:56:56.743 回答