Created
May 21, 2014 21:03
-
-
Save molpopgen/13ff677c18de50f731e6 to your computer and use it in GitHub Desktop.
Writes binary data to a gzipped file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//g++ -o readbgz readbgz.cc -lboost_iostreams -lz | |
#include <boost/iostreams/filtering_stream.hpp> | |
#include <boost/iostreams/device/file.hpp> | |
#include <boost/iostreams/filter/gzip.hpp> | |
#include <iostream> | |
#include <fstream> | |
#include <cstdlib> | |
#include <vector> | |
#include <zlib.h> | |
using namespace std; | |
void via_boost( const char * fn ) | |
/* | |
Uses boost's filtering_istream library. | |
Pro: this library is super-convenient via C++ synyax; | |
Con: streams are not seekable. Attempting in.seekg( pos ) | |
in the code below will put the stream into a bad state. | |
These scripts also cannot be opened in an append mode, if I recall correctly | |
*/ | |
{ | |
boost::iostreams::filtering_istream in; | |
in.push(boost::iostreams::gzip_decompressor()); | |
in.push(boost::iostreams::file_source("test.gz"), ios_base::in | ios_base::binary ); | |
unsigned nrecs; | |
in.read( reinterpret_cast<char *>(&nrecs), sizeof(unsigned) ); | |
cout << nrecs << '\n'; | |
vector<double> buffer2(nrecs); | |
in.read( reinterpret_cast<char *>(&buffer2[0]), nrecs*sizeof(double) ); | |
for(unsigned i=0;i<nrecs;++i) | |
{ | |
cout << buffer2[i] << '\n'; | |
} | |
in.pop(); | |
in.pop(); | |
} | |
/* | |
Uses the C-language zlib library | |
Pro: this library exists basically everywhere. Files are seekable, | |
although seeking is emulated and maybe very slow on some systems. | |
Files may be appended to. | |
Cons: the C-language syntax may be less convenient | |
*/ | |
void via_zlib( const char * fn ) | |
{ | |
gzFile gzin = gzopen("test.gz","r"); | |
unsigned nrecs; | |
int rv = gzread( gzin, &nrecs, sizeof(unsigned) ); | |
cout << nrecs << '\n'; | |
vector<double> buffer2(nrecs); | |
rv = gzread( gzin, &buffer2[0], nrecs*sizeof(double) ); | |
for(unsigned i=0;i<nrecs;++i) | |
{ | |
cout << buffer2[i] << '\n'; | |
} | |
gzclose(gzin); | |
} | |
int main( int argc, char ** argv ) | |
{ | |
cout << "Using boost:\n"; | |
via_boost("test.gz"); | |
cout << "Now, using zlib:\n"; | |
via_zlib("test.gz"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//g++ -o writebgz writebgz.cc -lz | |
#include <zlib.h> | |
#include <sstream> | |
#include <iostream> | |
using namespace std; | |
int main(int argc, char ** argv) | |
{ | |
ostringstream o; | |
unsigned nrecords=100; | |
o.write( reinterpret_cast<char *>(&nrecords), sizeof(unsigned) ); | |
for(unsigned i = 0 ; i < nrecords ; ++i ) | |
{ | |
double x = 1./double(i); | |
cerr << x << '\n'; | |
o.write( reinterpret_cast<char *>(&x), sizeof(double) ); | |
} | |
gzFile gzo = gzopen("test.gz","w"); | |
gzwrite( gzo, o.str().c_str(), o.str().size() ); | |
gzclose( gzo ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment