4

我正在考虑开始为 Linux 开发一个库,该库将为应用程序开发人员提供一个虚拟文件系统,其中文件将存储在存档中,并且存档中的每个文件都将单独压缩,因此检索单个文件非常简单对于开发人员、CPU 和硬盘驱动器而言,这是一项简单的任务。(无需复杂的 API,无需解压缩大量数据,只需解压缩相关数据,仅检索相关数据而非整个存档)

在 Linux 上使用 C++ 之前,我已经使用 popen 来检索命令的标准输出,但我不知道如何通过管道输入和获取数据,并且一些 bzip2 特定提示会很好。我在几年前写了一些类似的东西,但它包含一个作为 dll 的霍夫曼压缩库,而不是必须管道数据和使用标准工具。(那是在我的 Windows 时代。)

4

2 回答 2

4

bzip2 有一个库接口——这对您来说可能比调用子进程更容易。

我建议你也看看GIO 库,它已经是“应用程序开发人员的虚拟文件系统”;与从头开始编写库 VFS 相比,将其扩展为您想要的工作可能要少得多。

于 2011-11-16T01:34:03.097 回答
2

看看Boost IOStreams

例如,我从命令行创建了以下文件:

$ 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

然后我使用 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;
}

运行命令的结果就是解压出来的数据。

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

您当然没有逐个字符地读取数据,但我试图使示例保持简单。

于 2011-11-16T19:41:04.290 回答