我有一个代码正在运行,但我对结果并不完全满意,所以我想我可以在这里提出一些问题。
以下是我的两个功能:
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;
}
感谢。
答案 0 :(得分: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;
}
答案 1 :(得分:0)
我认为您可以将源文件的长度保存到压缩文件中。 解压缩时,可以通过压缩文件读取源文件的长度。
当然,您可以使用它进行压缩,这是我多次搜索的结果
dstLen = LZMA_PROPS_SIZE + srcLen + srcLen / 3 + 128