Skip to content

Instantly share code, notes, and snippets.

@willir
Created June 2, 2015 06:35
Show Gist options
  • Save willir/cdb0d395699b1944d339 to your computer and use it in GitHub Desktop.
Save willir/cdb0d395699b1944d339 to your computer and use it in GitHub Desktop.
zlib one hop compression
int deflateArr(const char *fileData, size_t fileDataSize) {
z_stream testStream;
uLong crc32Res = 0;
testStream.avail_in = (uInt)0;
testStream.total_in = 0;
testStream.total_out = 0;
testStream.data_type = Z_BINARY;
testStream.zalloc = Z_NULL;
testStream.zfree = Z_NULL;
testStream.opaque = Z_NULL;
int err = deflateInit2(&testStream, Z_BEST_SPEED, Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
if(Z_OK != err) {
cerr << "Error deflateInit2:" << err << endl;
return 1;
}
size_t outBufSize = deflateBound(&testStream, fileDataSize);
vector<uint8_t> bufferedData(outBufSize); /* buffer contain compressed data to be write*/
testStream.avail_out = (uInt)outBufSize;
testStream.next_out = bufferedData.data();
crc32Res = crc32(crc32Res, (const Bytef *) fileData, (uInt)fileDataSize);
testStream.next_in = (Bytef*)fileData;
testStream.avail_in = (uInt) fileDataSize;
err = deflate(&testStream, Z_FINISH);
ASSERT_X(Z_STREAM_END == err, "err:" << err);
ASSERT_X(0 == testStream.avail_in, "testStream.avail_in:" << testStream.avail_in);
ASSERT_X(testStream.total_out == bufferedData.size() - testStream.avail_out,
"testStream.total_out:" << testStream.total_out << " diff:" << bufferedData.size() - testStream.avail_out);
#if 0
size_t compressedSize = bufferedData.size() - testStream.avail_out;
cerr << "avail_out:" << testStream.avail_out << " diff:" << compressedSize << " total_out:" << testStream.total_out << endl;
#endif
size_t compressedSize = testStream.total_out;
deflateEnd(&testStream);
lock_guard<mutex> lck(mSaveToZipMutex);
fwrite(bufferedData.data(), 1, compressedSize, mTestFile);
fwrite(&crc32Res, 4, 1, mTestFile);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment