3

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;
}
4

1 に答える 1

1

特に LZMA については知りませんが、一般的な圧縮について知っていることから、解凍プログラムにビットストリームがどのように圧縮されているかを知らせるヘッダー情報なしで、圧縮されたビットストリームをファイルに書き込んでいるように見えます。

LzmaCompress() 関数は、おそらくこの情報を outProps に書き込みます。SDK には、圧縮されたビット ストリームを outBuffer に、プロパティを outProps に取り、それらから適切なアーカイブを作成する別の関数が必要です。

于 2009-12-09T17:09:16.783 に答える