如何在CBuilderXE中使用IdCompressZlib组件

时间:2012-12-13 07:30:12

标签: c++builder indy indy10

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

有人可以给我发一些关于如何使用这个主题的例子或一些有用的话题吗?

更新:下面是我尝试过的示例代码,当调用InflateStream方法时,它给出了errore -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()))));

2 个答案:

答案 0 :(得分: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.dat test2.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 ...

答案 1 :(得分: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;
        }
    }

}