Skip to content

Instantly share code, notes, and snippets.

@beakr
Created September 24, 2014 18:19
Show Gist options
  • Save beakr/1c1d24314287f46c17c8 to your computer and use it in GitHub Desktop.
Save beakr/1c1d24314287f46c17c8 to your computer and use it in GitHub Desktop.
Found a bunch of functions around the web and stuffed them into a little thing that does operations on GZip files.
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <zlib.h>
#include <zconf.h>
using namespace std;
void compress(string filename, string data)
{
const char * dat = data.c_str();
gzFile * f = (gzFile *) gzopen(filename.c_str(), "wb");
gzwrite(f, dat, strlen(dat));
gzclose(f);
}
bool inflateGzip(string &compressedBytes, string &uncompressedBytes)
{
if ( compressedBytes.size() == 0 ) {
uncompressedBytes = compressedBytes ;
return true ;
}
uncompressedBytes.clear() ;
unsigned full_length = compressedBytes.size() ;
unsigned half_length = compressedBytes.size() / 2;
unsigned uncompLength = full_length ;
char* uncomp = (char*) calloc( sizeof(char), uncompLength );
z_stream strm;
strm.next_in = (Bytef *) compressedBytes.c_str();
strm.avail_in = compressedBytes.size() ;
strm.total_out = 0;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
bool done = false ;
if (inflateInit2(&strm, (16+MAX_WBITS)) != Z_OK) {
free( uncomp );
return false;
}
while (!done) {
// If our output buffer is too small
if (strm.total_out >= uncompLength ) {
// Increase size of output buffer
char* uncomp2 = (char*) calloc( sizeof(char), uncompLength + half_length );
memcpy( uncomp2, uncomp, uncompLength );
uncompLength += half_length ;
free( uncomp );
uncomp = uncomp2 ;
}
strm.next_out = (Bytef *) (uncomp + strm.total_out);
strm.avail_out = uncompLength - strm.total_out;
// Inflate another chunk.
int err = inflate (&strm, Z_SYNC_FLUSH);
if (err == Z_STREAM_END) done = true;
else if (err != Z_OK) {
break;
}
}
if (inflateEnd (&strm) != Z_OK) {
free( uncomp );
return false;
}
for ( size_t i=0; i<strm.total_out; ++i ) {
uncompressedBytes += uncomp[ i ];
}
free( uncomp );
return true ;
}
bool getGzipBinary(const string& filename, string& contents) {
// Open the gzip file in binary mode
FILE* f = fopen( filename.c_str(), "rb" );
if ( f == NULL )
return false ;
// Clear existing bytes in output vector
contents.clear();
// Read all the bytes in the file
int c = fgetc( f );
while ( c != EOF ) {
contents += (char) c ;
c = fgetc( f );
}
fclose (f);
return true ;
}
int main()
{
compress("archive.gz", "Hello world!");
string binaryData;
if (!getGzipBinary("archive.gz", binaryData)) {
printf("Cannot get GZip binary data!\n");
exit(1);
}
string data;
if (!inflateGzip(binaryData, data)) {
printf("Cannot decompress GZip binary data!");
exit(1);
}
printf("Got data: ");
for (size_t i = 0; i < data.size(); ++i) {
printf("%c", data[i]);
}
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment