1

IdCompressZlibコンポーネントをCBuilderXEで使用する必要がありますが、そのテーマに関するドキュメントや例が見つかりませんでした。私はまた、失敗したそれを使用するためにいくつかの実験的な試みをします。

誰かが私にこのコンポーネントをどのように使用するか、またはそのようなトピックのためのいくつかの有用な好みについてのいくつかの例を投稿できますか?

更新:以下に、私が試したサンプルコードを示します。これにより、メソッドInflateStreamが呼び出されたときにエラー-5が発生します。

    int err;
String Fun = "[TestCompress] ", s1, zipString, strTest = "The 'zlib' compression library provides in-memory compression \
 and decompression functions, including integrity checks of the uncompressed data. \
 This version of the library supports only one compression method (deflation) \
 but other algorithms will be added later and will have the same stream interface. ";

TStringStream * inpStream = NULL, *outStream = NULL;
TMemoryStream * stream1   = NULL, *stream2 = NULL;

stream1 = new TMemoryStream();
stream2 = new TMemoryStream();

inpStream = new TStringStream();
outStream = new TStringStream();

inpStream->Clear();
inpStream->WriteString(strTest);
stream1->LoadFromStream(inpStream);
stream1->Position = 0;

IdCompressorZLib1->InflateStream(stream1, stream2);

outStream->Clear();
stream2->SaveToStream(outStream);
zipString = outStream->DataString;

MyLog(Fun + Format("Compress test: Compress size from [%d] to [%d]",
        ARRAYOFCONST((strTest.Length(), zipString.Length()))));
4

2 に答える 2

1

単純な文字列を解凍することはできません!
エラー: (-5)InflateStream最初からバイトまでを調べます:zlib: 78 01 or 78 9C or 78 DA
エラー: (-3)InflateStream最初からバイト (78 01) を見つけましたが、長さが一致しませんでした。
strTest では、最初のバイトは「Th」です。

次のコードで試してください (Rad 2010 Indy10 でテスト済み)

void __fastcall TForm1::Button1Click(TObject *Sender)
{

String strTest = "The 'zlib' compression library provides in-memory compression \
 and decompression functions, including integrity checks of the uncompressed data. \
 This version of the library supports only one compression method (deflation) \
 but other algorithms will be added later and will have the same stream interface. ";

TStringStream * inpStream = NULL;
TMemoryStream * stream1   = NULL, *stream2 = NULL, *stream3 = NULL;

stream1 = new TMemoryStream();
stream2 = new TMemoryStream();
stream3 = new TMemoryStream();

inpStream = new TStringStream();


inpStream->Clear();
inpStream->WriteString(strTest);
stream1->LoadFromStream(inpStream);
stream1->Position = 0;

// first compress stream1 with Level 1
IdCompressorZLib1->DeflateStream(stream1,stream2,1);

stream2->SaveToFile("test.dat");
stream3->LoadFromFile("test.dat");
stream2->Position = 0;
// Now decompress stream3
IdCompressorZLib1->InflateStream(stream3,stream2);
stream2->SaveToFile("test2.dat");

}

プログラムを実行すると、デバッグ フォルダにtest.dattest2.datの 2 つのファイルがあります。

test.dat 数バイト (サイズ 190)

xUQnÃ0C¯Â¿¶@× ... (Hex 78 01 55 8F 51 6E C3 ...)

test2.dat 数バイト (サイズ 306)

The 'zlib' compression library provides in-memory compression  and dec ...
于 2012-12-21T10:42:56.333 に答える
0

以下に作業サンプルがあります。提案してくれた皆さんに感謝します

void __fastcall TFormMain::btnTestIdCompressClick(TObject * Sender)
{
    int err;
    String Fun = "[TestIdCompress] ", s1, zipString, strTest = "The 'zlib' compression library provides in-memory compression \
     and decompression functions, including integrity checks of the uncompressed data. \
     This version of the library supports only one compression method (deflation) \
     but other algorithms will be added later and will have the same stream interface. ";

    TStringStream * inpStream = NULL, *outStream = NULL;
    TMemoryStream * stream1   = NULL, *stream2 = NULL;

    try
    {
        MyLog("--------------------------------------");
        MyLog(Fun + Format("Original msg : size=[%d] data=[%s]", ARRAYOFCONST((strTest.Length(), strTest))));

        stream1 = new TMemoryStream();
        stream2 = new TMemoryStream();

        inpStream = new TStringStream();
        outStream = new TStringStream();

        inpStream->Clear();
        inpStream->WriteString(strTest);
        stream1->LoadFromStream(inpStream);
        stream1->Position = 0;

        IdCompressorZLib1->DeflateStream(stream1, stream2, 1);

        outStream->Clear();
        stream2->SaveToStream(outStream);
        zipString = outStream->DataString;

        MyLog("--------------------------------------");
        MyLog(Fun + Format("Compressed msg : size=[%d] data=[%s]", ARRAYOFCONST((zipString.Length(), zipString))));

        inpStream->Clear();
        inpStream->WriteString(zipString);
        stream1->LoadFromStream(inpStream);
        stream1->Position = 0;

        stream2->Position = 0;
        // Now decompress stream3
        IdCompressorZLib1->InflateStream(stream1, stream2);
        outStream->Clear();
        stream2->SaveToStream(outStream);
        s1 = outStream->DataString;
        MyLog("--------------------------------------");
        MyLog(Fun + Format("Decompressed msg : size=[%d] data=[%s]", ARRAYOFCONST((s1.Length(), s1))));

    }
    __finally
    {
        if (stream1)
        {
            stream1->Clear();
            delete stream1;
            stream1 = NULL;
        }
        if (stream2)
        {
            stream2->Clear();
            delete stream2;
            stream2 = NULL;
        }
        if (inpStream)
        {
            inpStream->Clear();
            delete inpStream;
            inpStream = NULL;
        }
        if (outStream)
        {
            outStream->Clear();
            delete outStream;
            outStream = NULL;
        }
    }

}
于 2013-01-09T07:18:34.237 に答える