1

我需要将 IdCompressZlib 组件与 CBuilderXE 一起使用,但我没有找到关于该主题的文档或示例。我也做了一些尝试使用它失败了。

有人可以向我发布一些关于如何使用 thsi 组件或对此类主题有用的点赞的示例吗?

更新:下面是我尝试过的示例代码,当调用 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 回答