[英]How would I pipe data into bzip2 and get the resulting data from its stdout in C++ on Linux?
我正在考慮開始研究用於Linux的庫,它將為應用程序開發人員提供虛擬文件系統,其中文件將存儲在存檔中,並且存檔中的每個文件都將被單獨壓縮,以便檢索單個文件非常開發人員,CPU和硬盤驅動器的直接任務。 (沒有復雜的API,不需要解壓縮數據,只需要相關的數據,只檢索相關數據而不是整個存檔)
我在Linux上使用C ++之前已經使用popen來檢索命令的標准輸出,但是我不知道如何管理數據並獲取數據,並且一些bzip2特定的提示會很好。 我寫了類似於今年的東西,但它包括一個霍夫曼壓縮庫作為一個DLL,而不是必須管道數據和使用標准工具。 (那是在我的Windows時代。)
作為示例,我從命令行創建了以下文件:
$ 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
您當然沒有按字符讀取數據字符,但我試圖保持示例簡單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.