C++ C++;Boost和Lzma减压
我正在尝试使用解压缩.7z(或.xz或.lzma)文件C++ C++;Boost和Lzma减压,c++,boost,compression,lzma,C++,Boost,Compression,Lzma,我正在尝试使用解压缩.7z(或.xz或.lzma)文件 Linux平台上的boost library 1.67.0 使用以下代码: vector<T> readFromCompressedFile(string input_file_path, string output_file_path) { namespace io = boost::iostreams; stringstream strstream; ifstream file
- Linux平台上的boost library 1.67.0
vector<T> readFromCompressedFile(string input_file_path, string output_file_path)
{
namespace io = boost::iostreams;
stringstream strstream;
ifstream file(input_file_path.c_str(), ios_base::in | ios_base::binary);
ofstream out(output_file_path, ios_base::out | ios_base::binary);
boost::iostreams::filtering_istream in;
in.push(io::lzma_decompressor());
in.push(file);
io::copy(in, out);
cout<<strstream.str()<<endl;
vector readFromCompressedFile(字符串输入文件路径,字符串输出文件路径)
{
名称空间io=boost::iostreams;
stringstream和strstream;
ifstream文件(input_file_path.c_str(),ios_base::in | ios_base::binary);
输出流(输出文件路径,ios_base::out | ios_base::binary);
boost::iostreams::filtering\u istream in;
in.push(io::lzma_decompressor());
in.push(文件);
io::复制(输入、输出);
我可以证实同样的问题
使用其他工具解压缩lzma文件没有问题。可能存在版本控制问题,或者可能存在错误。以下是清理后的代码版本,没有太多噪音,消除了一些可疑的样式(使用命名空间std
),并尝试获取更多错误信息:
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/lzma.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/exception/diagnostic_information.hpp>
#include <fstream>
#include <iostream>
namespace io = boost::iostreams;
void foo(std::string input_file_path, std::string output_file_path) {
namespace io = boost::iostreams;
std::ifstream file(input_file_path, std::ios::binary);
std::ofstream out(output_file_path, std::ios::binary);
boost::iostreams::filtering_istreambuf in;
in.push(io::lzma_decompressor());
in.push(file);
try {
io::copy(in, out);
} catch(io::lzma_error const& e) {
std::cout << boost::diagnostic_information(e, true);
std::cout << e.code() << ": " << e.code().message() << "\n";
} catch(boost::exception const& e) {
std::cout << boost::diagnostic_information(e, true);
}
}
int main() {
foo("test.cpp.lzma", "output.txt");
}
#包括
#包括
#包括
#包括
#包括
#包括
名称空间io=boost::iostreams;
void foo(std::字符串输入\文件\路径,std::字符串输出\文件\路径){
名称空间io=boost::iostreams;
std::ifstream文件(输入文件路径,std::ios::binary);
std::ofstreamout(输出文件路径,std::ios::binary);
boost::iostreams::filtering_istreambuf in;
in.push(io::lzma_decompressor());
in.push(文件);
试一试{
io::复制(输入、输出);
}捕获(io::lzma_错误常量和e){
std::cout您的代码很好,这不是一个bug
起初,我遇到了与上述相同的问题,但经过一些研究,我发现这是因为boost iostreams库调用XZ库提供的lzma\u流解码器来执行解码工作,而.lzma或.7z格式文件不受lzma st支持ream_decoder。如果尝试使用boost iostreams库解码.lzma或.7z格式文件,将引发一个异常,错误代码为:lzma_format_error。请参阅XZ源代码中的错误代码定义
\src\liblzma\api\lzma\base.h
LZMA格式错误=7,
/**<
*\无法识别简短的文件格式
*
*解码器未将输入识别为支持的文件
*格式化。例如,在尝试
*使用lzma_流_解码器解码.lzma格式文件,
*因为lzma_流_解码器只接受.xz格式。
*/
请参考boost iostreams库的源代码:
您可以尝试解码.xz文件,不会有问题。
我已经用您在Windows X64和boost library 1.66.0上提供的相同代码对此进行了测试
顺便说一句,@sehe提供的错误检测代码具有误导性:
try {
io::copy(in, out);
} catch(io::lzma_error const& e) {
std::cout << boost::diagnostic_information(e, true);
std::cout << e.code() << ": " << e.code().message() << "\n";
}
试试看{
io::复制(输入、输出);
}捕获(io::lzma_错误常量和e){
非常感谢-这不仅仅是扩展的问题,而且xz
和lzma
是不同的压缩格式。
LZMA_FORMAT_ERROR = 7,
/**<
* \brief File format not recognized
*
* The decoder did not recognize the input as supported file
* format. This error can occur, for example, when trying to
* decode .lzma format file with lzma_stream_decoder,
* because lzma_stream_decoder accepts only the .xz format.
*/
try {
io::copy(in, out);
} catch(io::lzma_error const& e) {
std::cout << boost::diagnostic_information(e, true);
std::cout << e.code() << ": " << e.code().message() << "\n";
}
try {
io::copy(in, out);
} catch(io::lzma_error const& e) {
std::cout << boost::diagnostic_information(e, true);
std::cout << e.error() << ": " << e.code().message() << "\n";
}