使用boost将多个文件解压为一个文件
decompress multiple files in to one single file using boost
我有一组压缩文件。我必须解压缩所有文件并创建一个大文件。下面的代码工作得很好,但我不想使用std::stringstream,因为文件很大,我不想创建文件内容的中间副本。
如果我尝试直接使用boost::iostreams::copy(inbuf, tempfile);
,它会自动关闭文件(tmpfile)。有没有更好的复制内容的方法?或者至少,我能避免自动关闭这个文件吗?
std::ofstream tempfile("/tmp/extmpfile", std::ios::binary);
for (set<std::string>::iterator it = files.begin(); it != files.end(); ++it)
{
string filename(*it);
std::ifstream gzfile(filename.c_str(), std::ios::binary);
boost::iostreams::filtering_streambuf<boost::iostreams::input> inbuf;
inbuf.push(boost::iostreams::gzip_decompressor());
inbuf.push(gzfile);
//closes tempfile automatically!!
//boost::iostreams::copy(inbuf, tempfile);
std::stringstream out;
boost::iostreams::copy(inbuf, out);
tempfile << out.str();
}
tempfile.close();
我知道有很多方法可以让Boost IOStreams知道它不应该关闭流。我想它要求你使用boost::iostream::stream<>
而不是std::ostream
。
我的简单解决方法似乎是使用与单个std::filebuf
对象关联的临时std::ostream
:
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <set>
#include <string>
#include <iostream>
#include <fstream>
int main() {
std::filebuf tempfilebuf;
tempfilebuf.open("/tmp/extmpfile", std::ios::binary|std::ios::out);
std::set<std::string> files { "a.gz", "b.gz" };
for (std::set<std::string>::iterator it = files.begin(); it != files.end(); ++it)
{
std::string filename(*it);
std::ifstream gzfile(filename.c_str(), std::ios::binary);
boost::iostreams::filtering_streambuf<boost::iostreams::input> inbuf;
inbuf.push(boost::iostreams::gzip_decompressor());
inbuf.push(gzfile);
std::ostream tempfile(&tempfilebuf);
boost::iostreams::copy(inbuf, tempfile);
}
tempfilebuf.close();
}
Live On Coliru
使用像
这样的示例数据echo a > a
echo b > b
gzip a b
生成包含
的extmpfile
a
b
相关文章:
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 挂起和取消挂起一个文件DLL
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- C++试图读取一个文件并输出到另一个文本文件
- 如何从另一个文件继承私有成员变量和公共函数
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 如何打开并写入一个名称取自C++中字符串的文件
- 构建一个由C和C++文件组成的库
- c++多进程编写一个唯一的文件
- 如何使用CLion在Mac上创建一个新的.txt文件
- 如何将class.cpp和class.hpp编译为一个.o文件
- 写入文件会产生一个空文件或根本没有文件
- 如何将不同的可执行文件合并到一个窗口框架中进行编码?像浏览器一样
- 生成一个生成文件,该生成文件使用Automake在一个步骤中编译和链接所有源文件
- 为什么我的 heap.h 文件给我一个LNK2001错误?
- Pantheios 不使用 MFC 应用程序写入一个文件(一个 exe + 两个 DLL)
- 在C++中一次读取一个文件一个int
- 输入/输出文件(一个字母的大写)
- 如何使用两个预编译的头文件.一个用于托管代码,另一个用于非托管代码.xxx.PCH不是一个有效的预编译头文件