我正在嘗試使用LZMA SDK來創建zip存檔(zip或.7z格式)。我已經下載並構建了SDK,我只想使用dll導出來壓縮或解壓縮幾個文件。當我使用LzamCompress方法時,它返回0(SZ_OK),就像它正常工作一樣。但是,在將緩衝區寫入文件並嘗試將其打開後,出現文件無法作爲存檔打開的錯誤。您可以幫助您使用LZMA(7zip)SDK創建zip存檔嗎?
這是我目前使用的代碼。任何建議,將不勝感激。
#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;
}
你是如何打開文件的?您是否使用7zip歸檔程序或LZMA SDK中的其他功能? – 2009-12-09 16:53:16
7zip和Windows zip文件夾實用程序。 – scottm 2009-12-09 16:55:44
我也試圖做到這一點,但仍不成功。 需要注意的一些問題。1)首字節5個字節是標題屬性(outProps),接下來的8個字節是長度。 2)然後是壓縮數據。當我用這種方式寫的時候也失敗了。我想我們需要加'7','z'。我不確定。 – 2010-09-21 11:46:15