C++ 如果我试图将未压缩的过滤流复制到stringstream,则会崩溃
我想解压一个文件并将其内容写入stringstream 这是我尝试的代码:C++ 如果我试图将未压缩的过滤流复制到stringstream,则会崩溃,c++,segmentation-fault,gzip,boost-iostreams,C++,Segmentation Fault,Gzip,Boost Iostreams,我想解压一个文件并将其内容写入stringstream 这是我尝试的代码: string readGZipLog () { try { using namespace boost::iostreams; ifstream file(currentFile.c_str(), std::ios_base::in | std::ios_base::binary); boost::iostreams::filtering_istream in; in.p
string readGZipLog () {
try {
using namespace boost::iostreams;
ifstream file(currentFile.c_str(), std::ios_base::in | std::ios_base::binary);
boost::iostreams::filtering_istream in;
in.push(gzip_decompressor());
in.push(file);
std::stringstream strstream;
boost::iostreams::copy(in, strstream);
return strstream.str();
} catch (std::exception& e) {
cout << e.what() << endl;
}
}
void writeGZipLog (char* data) {
try {
using namespace boost::iostreams;
std::ofstream file( currentFile.c_str(), std::ios_base::out | std::ios_base::binary );
boost::iostreams::filtering_ostream out;
out.push( gzip_compressor() );
out.push(file);
std::stringstream strstream;
strstream << data;
boost::iostreams::copy( strstream, data );
} catch (std::exception& e) {
cout << e.what() << endl;
}
}
/build
是自动编译和启动应用程序/test
的脚本
我检查了文件:它包含一些内容,但我无法使用gunzip
将其解压缩。因此,我不确定压缩是否正常工作,以及这是否与Boost抛出的gzip错误有关
你能告诉我错误在哪里吗
谢谢你的帮助
Paul经过大量的研究和尝试,我终于找到了正确处理(反)压缩的方法
这是适用于我的代码,没有任何问题(使用gzip和bzip2):
string readgzip日志(){
使用名称空间boost::iostreams;
使用名称空间std;
试一试{
ifstream文件(currentFile.c_str(),ios_base::in | ios_base::binary);
boost::iostreams::filtering\u istream in;
in.push(gzip_decompressor());
in.push(文件);
stringstream和strstream;
boost::iostreams::copy(in,strstream);
返回strstrstream.str();
}捕获(const gzip_错误和异常){
库特
gzip error
./build: line 3: 22174 Segmentation fault ./test
string readGZipLog () {
using namespace boost::iostreams;
using namespace std;
try {
ifstream file(currentFile.c_str(), ios_base::in | ios_base::binary);
boost::iostreams::filtering_istream in;
in.push(gzip_decompressor());
in.push(file);
stringstream strstream;
boost::iostreams::copy(in, strstream);
return strstream.str();
} catch (const gzip_error& exception) {
cout << "Boost Description of Error: " << exception.what() << endl;
return "err";
}
}
bool writeGZipLog (char* data) {
using namespace boost::iostreams;
using namespace std;
try {
std::ofstream file( currentFile.c_str(), std::ios_base::app );
boost::iostreams::filtering_ostream out;
out.push( gzip_compressor() );
out.push(file);
stringstream strstream;
strstream << data;
boost::iostreams::copy(strstream, out);
return true;
} catch (const gzip_error& exception) {
cout << "Boost Description of Error: " << exception.what() << endl;
return false;
}
}