【问题标题】:Tried to decompress a zip file into memory with boost,but it doesn't work尝试使用 boost 将 zip 文件解压缩到内存中,但它不起作用
【发布时间】:2021-03-30 08:50:55
【问题描述】:

我尝试使用 boost zlib 解压缩一个 zip 文件,但它不起作用,我的 boost 版本是 1.75,它是已构建的二进制文件,我使用 VS 2013 CE 尝试了以下代码

 #include<iostream>
 #include<fstream>
 #include<algorithm> 
 #include<iterator>
 #include<sstream>
 #include <boost/iostreams/filtering_streambuf.hpp>
 #include <boost/iostreams/copy.hpp>
 #include <boost/iostreams/filter/zlib.hpp>

 using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    ifstream src;
    ofstream dst;
    stringstream  ss;

 try
 {
      src.open("d:\\202012303333629003180526491.txt.zip", ios::in | ios::binary);
      dst.open("to.zip", ios::out | ios::binary);

      copy(istreambuf_iterator<char>(src),
        istreambuf_iterator<char>(),
        ostreambuf_iterator<char>(ss));

      cout << ss.str() << endl;


      std::stringstream decompressed;

      boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
      in.push(boost::iostreams::zlib_decompressor());
      in.push(ss);
      boost::iostreams::copy(in, decompressed);
      string decompressestr = decompressed.str();

      src.close();
      dst.close();
      return 0;
  }
  catch (const std::exception& error)
  {
      std::cerr << error.what() << std::endl;
      return (EXIT_FAILURE);
   }
}

例外是 zlib 错误:iostream 流错误,知道我做错了什么吗?非常感谢

【问题讨论】:

  • boost 不支持zip 文件格式。您可以使用它来解压缩.zip 文件中的文件,但我发现直接使用zlib 库同样简单。如果您的问题是如何从 zip 中解压缩一个文件,我可以回答。 Here 就是我所做的一个例子。
  • @lakeweb 谢谢你的时间,我明白了

标签: c++ boost zlib boost-iostreams


【解决方案1】:

zlib 不处理 zip 文件。 zlib 可以处理 zlib、gzip 和原始 deflate 流。

【讨论】:

  • 真的很欣赏它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-23
  • 1970-01-01
  • 1970-01-01
  • 2018-06-16
  • 1970-01-01
  • 2018-10-21
  • 1970-01-01
相关资源
最近更新 更多