C++: GZ-Dateien on-the-fly dekomprimieren mit boost::iostreams
English
Deutsch
Dieses minimale Beispiel zeigt Ihnen, wie Sie eine .gz-Datei in C++ öffnen, sie on-the-fly mit boost::iostreams dekomprimieren und dann ihren Inhalt nach stdout kopieren:
iostreams_gz_decompress.cpp
#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
using namespace std;
int main(int argc, char** argv) {
if(argc < 2) {
cerr << "Verwendung: " << argv[0] << " <gzip-Eingabedatei>" << endl;
}
//Aus dem ersten Befehlszeilenargument lesen, annehmen dass es gzip-komprimiert ist
ifstream file(argv[1], ios_base::in | ios_base::binary);
boost::iostreams::filtering_streambuf<boost::iostreams::input> inbuf;
inbuf.push(boost::iostreams::gzip_decompressor());
inbuf.push(file);
//Streambuf in istream umwandeln
istream instream(&inbuf);
//Alles von instream nach stdout kopieren
cout << instream.rdbuf();
//Aufräumen
file.close();
}CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
find_package(Boost 1.36.0 COMPONENTS iostreams)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(iostreams-gz-decompress iostreams-gz-decompress.cpp)
target_link_libraries(iostreams-gz-decompress ${Boost_LIBRARIES})If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow