可以提升iostreams即时读取和压缩gzip文件吗?
问题描述:
我读使用升压输入输出流gzip压缩的文件: 以下工作正常:可以提升iostreams即时读取和压缩gzip文件吗?
namespace io = boost::iostreams;
io::filtering_istream in;
in.push(boost::iostreams::basic_gzip_decompressor<>());
in.push(io::file_source("test.gz"));
stringstream ss;
copy(in, ss);
不过,我不希望采取的读取整个gzip文件 到内存内存命中。我希望能够逐步读取文件。
例如,如果我有一个数据结构X从istream的初始化自身,
X x;
x.read(in);
失败。大概这是因为如果我们正在做部分流,我们可能不得不将字符放回流 。任何想法是否提升iostreams支持这个?
答
根据iostream documentation的类型boost::io::filtering_istream
派生自std::istream
。也就是说,应该有可能通过这个无处不在std::istream&
预计。如果因为需要unget()
或putback()
个字符而在运行时出现错误,则应该查看pback_size
参数,该参数指定最多返回多少个字符。我没有在文档中看到这个参数的默认值是什么。
如果这不能解决你的问题,你能描述一下你的问题到底是什么吗?从它的外观应该工作。
答
我认为你需要编写你自己的过滤器。例如,要仔细阅读说明书是.tar.gz和输出文件,我写了类似
//using namespace std;
namespace io = boost::iostreams;
struct tar_expander
{
tar_expander() : out(0), status(header)
{
}
~tar_expander()
{
delete out;
}
/* qualify filter */
typedef char char_type;
struct category :
io::input_filter_tag,
io::multichar_tag
{ };
template<typename Source>
void fetch_n(Source& src, std::streamsize n = block_size)
{
/* my utility */
....
}
// Read up to n filtered characters into the buffer s,
// returning the number of characters read or -1 for EOF.
// Use src to access the unfiltered character sequence
template<typename Source>
std::streamsize read(Source& src, char* s, std::streamsize n)
{
fetch_n(src);
const tar_header &h = cast_buf<tar_header>();
int r;
if (status == header)
{
...
}
std::ofstream *out;
size_t fsize, stored;
static const size_t block_size = 512;
std::vector<char> buf;
enum { header, store_file, archive_end } status;
}
}
我的功能read(Source &...)
时调用接收解压缩后的文本。 使用的过滤器:
ifstream file("/home/..../resample-1.8.1.tar.gz", ios_base::in | ios_base::binary);
io::filtering_streambuf<io::input> in;
in.push(tar_expander());
in.push(io::gzip_decompressor());
in.push(file);
io::copy(in, cout);
会做的操作,如调用'函数getline()''然后压缩()'它通过你的循环工作? – user99545 2012-02-28 20:28:50
@ user99545:否:因为X根据二进制数据创建自己。 – ATemp 2012-02-28 20:30:47
我不明白为什么不。我已经使用boost iostreams以这种方式读取和写入zlib压缩的流。 – Ferruccio 2012-02-28 21:29:34