How can I transfer data to bzip2 and get the received data from its stdout in C ++ on Linux?

I am considering starting work on a library for Linux that will provide a virtual file system for application developers, where files will be stored in the archive, and each file in the archive will be individually compressed to extract one file, this is a very simple task for the developer, for the processor and hard disk . (There is no complex API, there is no need to unpack data summaries, as well as data relevant and retrieving only relevant data, and not the entire archive)

I used popen to extract the stdout command before using C ++ here on Linux, but I don't know how to transfer data and receive data, and some special bzip2 tips will be nice. I wrote something similar to these years ago, but it included a huffman compression library as a dll, instead of transferring data and using a standard tool. (that was in my days on Windows.)

+4
source share
2 answers

bzip2 has a library interface - this will probably be easier for you than invoking a subprocess.

I also recommend that you familiarize yourself with the GIO library, which is already a "virtual file system for application developers"; it can be much less to extend it to do what you want than write a VFS library from scratch.

+4
source

Check out the 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 

Then I used boost :: iostreams :: filtering_istream to read the results of the unzipped bzip2 file named file.bz2.

 #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 the command is decompressed data.

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

Of course, you did not read the data symbol by nature, but I tried to keep this example simple.

+2
source

Source: https://habr.com/ru/post/1381410/


All Articles