使用boost :: bzip2_decompressor()时如何摆脱额外的值

时间:2015-07-09 15:19:32

标签: c++ gcc boost iostream

我有一个txt文件:gcc-4.7.2.txt:写入数据:Hello这是一个测试文件。谢谢:压缩为gcc-4.7.2.tar.bz2

现在,我运行以下代码:

#include <sstream>
#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
#include <boost/filesystem.hpp>

int main()
{
    using namespace std;
    using namespace boost::iostreams;

    char filename[] = "gcc-4.7.2.tar.bz2";

    if (!boost::filesystem::exists(filename))
    {
        cout << "Can't find " << filename << ". Expect errors to follow! " << endl;
    }

    ifstream file(filename, ios_base::in | ios_base::binary);
    filtering_streambuf<input> in;
    filtering_streambuf<output> out;

    in.push(bzip2_decompressor());
    in.push(file);

    try
    {
        //cout <<  "in_file:" << in << endl;
        boost::iostreams::copy(in, cout);
        //boost::iostreams::copy(in, out);
        //cout << cout << endl;
        //boost::iostreams::copy(in, compressed_string);
        //cout << "Copied" << compressed_string << "  " <<  in.str() << endl;
    }
    catch (const bzip2_error& exception)
    {
        cout << "catchblock" << endl;
        cout << exception.what() << endl;
        int error = exception.error();
        if (error == bzip2::data_error)
        {
            cout << "compressed data stream is corrupted";
        }
        else if (error == bzip2::data_error_magic)
        {
            cout << "compressed data stream does not begin with the 'magic' sequence 'B' 'Z' 'h'";
        }
        else if (error == bzip2::config_error)
        {
            cout << "libbzip2 has been improperly configured for the current platform";
        }
        else
        {
            cout << "Error: " << error;
        }
        cout << endl;
    }
}

运行时输出为:

dev4 @ sun-desktop:〜/ readerwriter $ ./test1 gcc-4.7.2.txt0000644000175100001440000000004312547435102011603 0ustar dev4usersHello 这是一个测试文件。

由于

Hello之前有哪些字符?为什么打印文件名?

如何摆脱这些额外的价值观。 并且只打印文件的内容:

您好 这是一个测试文件。

由于

1 个答案:

答案 0 :(得分:0)

你做不到。

这些不是“额外的价值”。

  • 如果您不想使用tar存档,请不要使用tar。

      

    相反,使用setlocal EnableDelayedExpansion rem Servers with 3 passwords: for %%a in (server1 server2 server27) do set arrayserver[%%a]=3 rem Servers with 2 passwords: for %%a in (serverA serverB serverZ) do set arrayserver[%%a]=2 set /p server=Enter server: goto passwords-!arrayserver[%server%]! 压缩单个文件

  • 如果输入是tar存档,请使用bzip2解压缩。

      

    Boost 支持tar档案

相关问题