简体   繁体   English

我如何将数据传输到bzip2并在Linux上用C ++从stdout获取结果数据?

[英]How would I pipe data into bzip2 and get the resulting data from its stdout in C++ on Linux?

I am considering beginning work on a library for Linux that would provide a virtual file system to application developers where the files would be stored in an archive, and each file within the archive would be individually compressed so that retrieval of a single file is a very straightforward task for the developer, for the CPU, and the hard drive. 我正在考虑开始研究用于Linux的库,它将为应用程序开发人员提供虚拟文件系统,其中文件将存储在存档中,并且存档中的每个文件都将被单独压缩,以便检索单个文件非常开发人员,CPU和硬盘驱动器的直接任务。 (No complicated API, no need to uncompress gigs of data, just the data that's relevant, and retrieval of only relevant data rather than the whole archive) (没有复杂的API,不需要解压缩数据,只需要相关的数据,只检索相关数据而不是整个存档)

I've used popen to retrieve the stdout of a command before using C++ here on Linux, but I don't know how to pipe data in and get data out, and some bzip2 specific tips would be nice. 我在Linux上使用C ++之前已经使用popen来检索命令的标准输出,但是我不知道如何管理数据并获取数据,并且一些bzip2特定的提示会很好。 I wrote something similar to this years ago, but it included a huffman compression library as a dll, rather than having to pipe data and use a standard tool. 我写了类似于今年的东西,但它包括一个霍夫曼压缩库作为一个DLL,而不是必须管道数据和使用标准工具。 (that was back in my Windows days.) (那是在我的Windows时代。)

bzip2 has a library interface -- that will probably be easier for you than invoking a subprocess. bzip2有一个库接口 - 这可能比调用子进程更容易。

I recommend you also have a look at the GIO library , which is already a "virtual file system for application developers"; 我建议你也看一下GIO库 ,它已经是“面向应用程序开发人员的虚拟文件系统”; it might be a lot less work to extend that to do what you want, than to write a library VFS from scratch. 扩展它以做你想做的事情可能要少得多,而不是从头开始编写库VFS。

Have a look at Boost IOStreams 看看Boost IOStreams

As an example I created the following file from the command line: 作为示例,我从命令行创建了以下文件:

$ echo "this is the first line" > file
$ echo "this is the second line" >> file
$ echo "this is the third line" >> file
$ bzip2 file 
$ file file.bz2 
file.bz2: bzip2 compressed data, block size = 900k

I then used a boost::iostreams::filtering_istream to read the results of the decomressed bzip2 file named file.bz2. 然后我使用boost :: iostreams :: filtering_istream来读取名为file.bz2的已解压缩的bzip2文件的结果。

#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <iostream>

namespace io = boost::iostreams;

/* To Compile:
g++ -Wall -o ./bzipIOStream ./bzipIOStream.cpp -lboost_iostreams
*/

int main(){

    io::filtering_istream in;
    in.push(io::bzip2_decompressor());
    in.push(io::file_source("./file.bz2"));

    while(in.good()){
        char c = in.get();
        if(in.good()){
            std::cout << c;
        }
    }

    return 0;
}

The result of of running the command is the decompressed data. 运行该命令的结果是解压缩的数据。

$ ./bzipIOStream 
this is the first line
this is the second line
this is the third line

You don't have read the data character by character of course but I was trying to keep the example simple. 您当然没有按字符读取数据字符,但我试图保持示例简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM