我正在尝试使用LZMA SDK创建一个zip存档(.zip或.7z格式)。我已经下载并构建了SDK,我只想使用dll导出来压缩或解压缩一些文件。当我使用LzamCompress方法时,它返回0(SZ_OK),就像它正常工作一样。但是,在我将缓冲区写入文件并尝试打开它之后,我收到一个错误,即该文件无法作为存档打开。
这是我目前使用的代码。任何建议将不胜感激。
#include "lzmalib.h"
typedef unsigned char byte;
using namespace std;
int main()
{
int length = 0;
char *inBuffer;
byte *outBuffer = 0;
size_t outSize;
size_t outPropsSize = 5;
byte * outProps = new byte[outPropsSize];
fstream in;
fstream out;
in.open("c:\\temp\\test.exe", ios::in | ios::binary);
in.seekg(0, ios::end);
length = in.tellg();
in.seekg(0, ios::beg);
inBuffer = new char[length];
outSize = (size_t) length / 20 * 21 + ( 1 << 16 ); //allocate 105% of file size for destination buffer
if(outSize != 0)
{
outBuffer = (byte*)malloc((size_t)outSize);
if(outBuffer == 0)
{
cout << "can't allocate output buffer" << endl;
exit(1);
}
}
in.read(inBuffer, length);
in.close();
int ret = LzmaCompress(
outBuffer, /* output buffer */
&outSize, /* output buffer size */
reinterpret_cast<byte*>(inBuffer),/* input buffer */
length, /* input buffer size */
outProps, /* archive properties out buffer */
&outPropsSize,/* archive properties out buffer size */
5, /* compression level, 5 is default */
1<<24,/* dictionary size, 16MB is default */
-1, -1, -1, -1, -1/* -1 means use default options for remaining arguments */
);
if(ret != SZ_OK)
{
cout << "There was an error creating the archive." << endl;
exit(1);
}
out.open("test.zip", ios::out | ios::binary);
out.write(reinterpret_cast<char*>(outBuffer), (int)(outSize));
out.close();
delete inBuffer;
delete outBuffer;
}
答案 0 :(得分:1)
我不是特别了解LZMA,但从我对压缩的了解一般来看,看起来你正在写一个压缩的比特流到一个文件而没有任何头信息让解压缩程序知道比特流是怎么回事压缩。
LzmaCompress()函数可能会将此信息写入outProps。 SDK中应该有另一个函数,它将获取outBuffer中的压缩位流和outProps中的属性,并从中创建适当的存档。