2

次のコードを使用して、ブースト内のgzipされた文字列を解凍しようとしています

std::string DecompressString(const std::string &compressedString)
{
    std::stringstream src(compressedString);
    if (src.good())
    {
    boost::iostreams::filtering_streambuf<boost::iostreams::input> in(src);
    std::stringstream dst;
    boost::iostreams::filtering_streambuf<boost::iostreams::output> out(dst);
    in.push(boost::iostreams::zlib_decompressor());

    boost::iostreams::copy(in, out);
    return dst.str();
    }
    return "";

}

ただし、この関数を呼び出すたびに (以下のように)

string result = DecompressString("H4sIA");
string result = DecompressString("H4sIAAAAAAAAAO2YMQ6DMAxFfZnCXOgK9AA9ACsURuj9N2wpkSIDootxhv+lN2V5sqLIP0T55cEUgdLR48lUgToTjw/5zaRhBuVSKO5yE5c2kDp5zunIaWG6mz3SxLvjeX/hAQ94wAMe8IAHPCwyMS9mdvYYmTfzdfSQ/rQGjx/t92A578l+T057y1Ff6NW51Uy0h+zkLZ33ByuPtB8IuhdcnSMIglgm/r15/rtJctlf4puMt/i/bN16EotQFgAA");

プログラムは常にこの行で失敗します

in.push(boost::iostreams::zlib_decompressor());

次の例外を生成します

Unhandled exception at 0x7627b727 in KHMP.exe: Microsoft C++ exception: 
boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::logic_error> > at memory location 0x004dd868..

これについてはまったくわかりません...誰か提案はありますか?

ありがとう

編集:

提案に従って、コードを次のように切り替えます

 boost::iostreams::filtering_streambuf<boost::iostreams::input> in;//(src);

    in.push(boost::iostreams::zlib_decompressor());
    in.push(src);
     std::stringstream dst;
    boost::iostreams::filtering_streambuf<boost::iostreams::output> out;//(dst);
    out.push(dst);
    boost::iostreams::copy(in, out);

ただし、コピー時に発生することを除いて、例外は引き続き発生します

4

2 に答える 2

2

brado86 のアドバイスに従った後zlib_decompressor()gzip_decompressor()

于 2013-02-17T05:39:14.283 に答える
1

フィルターを間違った順序でプッシュしているようです。

Boost.Iostreams のドキュメントから理解できることから、入力の場合、データはフィルターをプッシュした逆の順序でフィルターを通過します。したがって、次の行を次のように変更すると、うまくいくはずです。

変化する

boost::iostreams::filtering_streambuf<boost::iostreams::input> in(src);
std::stringstream dst;
boost::iostreams::filtering_streambuf<boost::iostreams::output> out(dst);
in.push(boost::iostreams::zlib_decompressor());

boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
in.push(boost::iostreams::zlib_decompressor());
in.push(src);        // Note the order of pushing filters into the instream.
std::stringstream dst;
boost::iostreams::filtering_streambuf<boost::iostreams::output> out(dst);

詳細については、Boost.Iostreams のドキュメントを参照してください。

于 2011-03-22T04:48:10.237 に答える